Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function after leaving input field

Tags:

On a contact form, I have several input fields. One of those fields, is an email address field:

<input type='text' name='input_email' onChange={this.validateEmail}/> 

Right now, I have the email validation function set to the onChange attribute. Thus, while the user is entering characters into the email field, an error message appears the whole time.

I want to change this, so that this.validateEmail only gets called once when the user leaves that specific input field. How would I accomplish this?

I can't find any default object events that would solve this issue.

FYI, using ReactJS

like image 900
hammerabi Avatar asked Aug 05 '16 14:08

hammerabi


People also ask

How do you call a function after input?

You can use onblur() or onfocusout() . It will call function once you click out of that text field.

Which event gets triggered when user leaves an input field?

Definition and Usage. The onblur attribute fires the moment that the element loses focus. Onblur is most often used with form validation code (e.g. when the user leaves a form field).

How do you restrict input input field?

The HTML <input> tag is used to get user input in HTML. To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.


1 Answers

You can use onblur() or onfocusout(). It will call function once you click out of that text field.

Example:

function myFunction() {      var x = document.getElementById("sometext");      x.value = x.value.toUpperCase();  }
<input type="text" id="sometext" onfocusout="myFunction()">
like image 121
Aleksandar Đokić Avatar answered Oct 14 '22 01:10

Aleksandar Đokić