Skip to Content

searchTriggerOnlyChars()

Boolean check if the pressed key is a letter or number.

JavaScript February 22, 2017

Usage

JavaScript
searchTriggerOnlyChars(e)

Parameters

e
(Required) (Object) The keypress event
Default: None

Parameter Notes

Call this function inside of a keypress to test the key.

Request or provide clarification »

Examples

JavaScript
jQuery("input#whatever").on('keypress paste', function(e){
    if ( searchTriggerOnlyChars(e) ){
        //Do stuff if the key was a letter or number
    }
});

Additional Notes

This allows lowercase or uppercase letters or numbers.

Source File

Located in /assets/js/nebula.js on line 2051.

Note: This function contains 1 to-do comment.

JavaScript
nebula.searchTriggerOnlyChars = function(e){
    //@TODO "Nebula" 0: This still allows shortcuts like "cmd+a" to return true.
    var spinnerRegex = new RegExp("^[a-zA-Z0-9]+$");
    var allowedKeys = [8, 46];
    var searchChar = String.fromCharCode(!e.charCode ? e.which : e.charCode);

    if ( spinnerRegex.test(searchChar) || allowedKeys.indexOf(e.which) > -1 ){
        return true;
    } else {
        return false;
    }
};

Override

To override or disable this JavaScript function, simply redeclare it with the exact same function name.

JavaScript
nebula.searchTriggerOnlyChars = function(e){
    //Write your own code here, leave it blank, or return false.
}