Search code examples
jquerykeyboard-eventscustom-events

How to create a custom event in jquery


I want to create a custom event that will be triggered whenever a numeric value is entered in an input type="text". Alphabets should be ignored. change and blur events didn't work well for me.

Any suggestions? Thanks.


Solution

  • You might still be able to use change or blur events, but insert some additional logic to detect if your input is something that you'd want to trigger an event with, so

    $(".field").on("change blur keydown keyup", function(){
        if($(this).val().match(/^\d+$/){
          //your code
        } else {
          //do nothing
        }
    
    });