JS code validated when the mouse focus leaves the text box

Source: Internet
Author: User

The simplest I've found an example using JS onblur

The onblur event is the event that occurs when the cursor loses focus.

Cases

1.html

The code is as follows Copy Code

<input type=text name=text1 value= "1111onblur_green" onblur= "(document.bgcolor= ' green ')" >
<input type=text name=text2 value= "2222onblur_black" onblur= "(document.bgcolor= ' black ')" >
<input type=text name=text2 value= "3333onblur_yellow" onblur= "(document.bgcolor= ' yellow ')" >


The above is written directly in HTML input, we can be defined as a function

The code is as follows Copy Code
<script>
function Onblurs () {
if (frm.name.value== "") {
Alert ("Please enter your name!") ");
}else if (frm.funny.value== "") {
Alert ("Hobby must not be empty!") ");
}
}
</script>
<form name= "frm" >
<tr>
<td> Your name:</td>
<td><input type= "text" id= "name" onblur= "Onblus ()"/></td>
</tr><tr>
<td> Your hobby:</td>
<td><input type= "text" id= "funny" onblur= "Onblus ()"/></td>
</tr>
</form>

The above JS verification is almost, we use jquery blur for Ajax verification, below is my work example

Html

The code is as follows Copy Code

<input name= "Tgmo" type= "text" class= "TCINP" id= "Tgmo" size= "/>"

$ (' #tgmo '). blur (function () {
Alert ($ (this). Val ());
$.post (' Post.php?action=check ', {' Tgmo ': $ (this). Val ()},function (data)
{
alert (data);
if (data==0)
{
$ (' #sy_a '). Show ();
$ (' #autoregister '). val (1);
}
Else
{
$ (' #sy_a '). Hide ();
$ (' #autoregister '). val (0);
}

});
})

post.php

is to get the phone for verification, return the corresponding data, such as we can directly input

Echo 1;


About the jquery blur () method

On top our blur just binds the event directly through jquery, which makes it a lot easier to change its color when the input field loses focus (blur):

Definitions and usage
The Blur event occurs when the element loses focus.

The blur () function triggers the blur event, or if a function parameter is set, it can also specify the code to execute when the Blur event occurs.

Tip: Earlier, the Blur event occurred only on form elements. In a new browser, this event can be used for any element.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.