A validation instance method of jquery

Source: Internet
Author: User

The previous article introduced JS's regular verification method, this one is using jquery to run some of the methods of the example

JS section

<script>

$ (function () {

var Ok1=false;

var Ok2=false;

var Ok3=false;

var Ok4=false;

Verify User Name

$ (' input[name= ' username "]). focus (function () {

$ (this). Next (). Text (' User name should be between 3-20 bits '). Removeclass (' state1 '). addclass (' State2 ');

}). blur (function () {

if ($ (this). Val (). length >= 3 && $ (this). Val (). Length <=12 && $ (this). val () = ") {

$ (this). Next (). Text (' input succeeded '). Removeclass (' state1 '). addclass (' state4 ');

Ok1=true;

}else{

$ (this). Next (). Text (' User name should be between 3-20 bits '). Removeclass (' state1 '). addclass (' State3 ');

}

});

Verify Password

$ (' input[name= ' password "]). focus (function () {

$ (this). Next (). Text (' Password should be between 6-20 bits '). Removeclass (' state1 '). addclass (' State2 ');

}). blur (function () {

if ($ (this). Val (). length >= 6 && $ (this). Val (). Length <=20 && $ (this). val () = ") {

$ (this). Next (). Text (' input succeeded '). Removeclass (' state1 '). addclass (' state4 ');

Ok2=true;

}else{

$ (this). Next (). Text (' Password should be between 6-20 bits '). Removeclass (' state1 '). addclass (' State3 ');

}

});

Verification Confirmation Password

$ (' input[name= ' Repass "]). focus (function () {

$ (this). Next (). Text (' Enter the confirmation password to be the same as the above password, the same rules will be the same '). Removeclass (' state1 '). addclass (' State2 ');

}). blur (function () {

if ($ (this). Val (). length >= 6 && $ (this). Val (). Length <=20 && $ (this). val ()! = "&& $ (this) . val () = = $ (' input[name= "password"]). Val ()) {

$ (this). Next (). Text (' input succeeded '). Removeclass (' state1 '). addclass (' state4 ');

Ok3=true;

}else{

$ (this). Next (). Text (' Enter the confirmation password to be the same as the above password, the same rules will be the same '). Removeclass (' state1 '). addclass (' State3 ');

}

});

Verify Mailbox

$ (' input[name= ' email "]). focus (function () {

$ (this). Next (). Text (' Please enter the correct email format '). Removeclass (' state1 '). addclass (' State2 ');

}). blur (function () {

if ($ (this). Val (). Search (/\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) */) ==-1) {

$ (this). Next (). Text (' Please enter the correct email format '). Removeclass (' state1 '). addclass (' State3 ');

}else{

$ (this). Next (). Text (' input succeeded '). Removeclass (' state1 '). addclass (' state4 ');

Ok4=true;

}

});

Submit button, all validation passed by party can submit

$ ('. Submit '). Click (function () {

if (Ok1 && ok2 && ok3 && ok4) {

$ (' form '). Submit ();

}else{

return false;

}

});

});

</script>

<form action= ' do.php ' method= ' post ' >

User name: <input type= "text" name= "username" >

<span class= ' state1 ' > Please enter your username </span><br/><br/>

Password: <input type= "password" name= "password" >

<span class= ' state1 ' > Please enter your password </span><br/><br/>

Confirm Password: <input type= "password" name= "Repass" >

<span class= ' state1 ' > Please enter your confirmation password </span><br/><br/>

Email: <input type= "text" name= "email" >

<span class= ' state1 ' > Please enter your email address </span><br/><br/>

<a href= "javascript:;" ></a>

</form>

A validation instance method of jquery

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.