How to use jquery to check input field on change?

I have an input field:

 <input type="text" name="notifyEmail" id="parametersEmail" value="" size=40 /> 

I have a piece of jquery code that works when I click on a tab or otherwise leave a field that invokes the validation procedure:

 $("#parametersEmail").blur(function(event) { validateParameterEmail(); }); 

What I would like to do is run the validateParameterEmail() function whenever the value or contents of the input field changes.

So, I also tried the .change() handler:

 $("#parametersEmail").change(function(event) { validateParameterEmail(); }); 

But when I change the contents of parametersEmail , this handler does not call the validation function.

Is there any other handler I should use? Or can I not connect multiple event handlers to the input field?

+8
jquery input validation onchange blur
source share
4 answers

Try $("#parametersEmail").keydown(function(event) {})

+8
source share

Try the following:

 $("#parametersEmail").bind('blur', function(event) {} ); 

and

 $("#parametersEmail").bind('keyup', function(event) {} ); 
+6
source share

(Reality almost in 2017)

The best way is to set the callback once in all cases where you need to change the input value.

There is:

  • keyup: user typed input
  • change: a well-known method for saving input changes
  • click: some inputs accept changes with the mouse.
  • paste: Yes! Ctrl + V, Crtl + Ins, RightMouseButton + Paste can also change the value of the input.
  • propertychange: when some JS changes our input in any way, this event will be fired
  • input: a new standard event that supports all modern browsers, also uses it

So, we can install all of them once:

 $("#parametersEmail").bind("propertychange change click keyup input paste", function(event) { validateParameterEmail(); }); 

Hope this helps someone.;)

+2
source share

The change refers to when you press / move from the input. Perhaps you are looking for onKeyUp() .

0
source share

All Articles