JQ Combat-form validation

Source: Internet
Author: User

As a record of learning, put hundred people to view, nonsense not much to say, directly on the code

HTML structure:

<form action="a.php"Method="" class="form"><div><label for="username"> User name: </label><input id="username" class="Required"Type="text"/></div><div><label for="Email"> e-mail: </label><input id="Email" class="Required"Type="text"/></div><div><label for="Personinfo"> Profile: </label><input id="Personinfo" class="Required"Type="text"/></div><div><input id="Send"Type="Submit"/></div></form><form action="a.php"Method="" class="form"><div><label for="username"> User name: </label><input id="username" class="Required"Type="text"/></div><div><label for="Email"> e-mail: </label><input id="Email" class="Required"Type="text"/></div><div><label for="Personinfo"> Profile: </label><input id="Personinfo" class="Required"Type="text"/></div><div><input id="Send"Type="Submit"/></div></form>

JQ Code:

$("form:input.required"). each (function () {var$required = $ ('<strong class= "High" >*</strong>');$( This). Parent (). append ($required);}); $("Form:input"). blur (function () {var$parent = $ ( This). Parent (); $parent. Find (". Formtips"). Remove ();//Remove redundant Duplicates//Verify user nameif($( This). is('#username')){if( This. Value = =""|| This.value.length<6 ){varErrorMsg ="Please enter a 6-digit user name"; $parent. Append ($ ('<span class= "Formtips onError" >'+errormsg+'</span>'));}Else{varOkmsg ="Enter the correct"; $parent. Append ($ ('<span class= "Formtips okmsg" >'+okmsg+'</span>'));}}//Verify Mailboxif($( This). is('#email')){if( This. Value = =""){//|| (this.value!= "" &&!/[email protected]+\. [A-za-z] {2,4}$/.test (This.value)varErrorMsg ="Please enter the correct e-mail"; $parent. Append ($ ('<span class= "Formtips onError" >'+errormsg+'</span>'));}Else{varOkmsg ="Enter the correct"; $parent. Append ($ ('<span class= "Formtips okmsg" >'+okmsg+'</span>'));}}}). KeyUp (function () {$ ( This). Triggerhandler ('Blur');}). Focus (function () {$ ( This). Triggerhandler ('Blur');}); $("#send"). Click (function () {$ ("form. Required:input"). Trigger ('Blur');varNumerror = $ ("form. OnError"). length;var$id = $ ("form. OnError"). Prevall (". Required"). attr ("ID");if(numerror) {if(Numerror >1){$("#username"). focus ();}Else if($id = ="Email"){$("#email"). focus ();}return false;} Alert ("registration is successful, the password has been sent to your mailbox, please find");});

Learning the code needs to run a few more times!

JQ Combat-form validation

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.