An example of a simple form validation in jquery

Source: Internet
Author: User

The HTML code is as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<link href= "Css/style.css" rel= "stylesheet" type= "Text/css"/>
<!--Introducing jquery--
<script src= ". /scripts/jquery-1.3.1.js "type=" Text/javascript "></script>
<script type= "Text/javascript" >
//<! [cdata[
$ (function () {
//If required, add Red Star logo
$ ("form:input.required"). each (function () {
var $required = $ ("<strong class= ' High ' > *</strong>");//Create elements
$ (this). Parent (). append ($required);//And append it to the document
});
//After the text box loses focus
$ (' Form:input '). blur (function () {
var $parent = $ (this). parent ();
$parent. Find (". Formtips"). Remove ();
//Verify user name
if ($ (this). is (' #username ')) {
if (this.value== "" | | This.value.length < 6) {
var errormsg = ' Please enter a user name of at least 6 bits. ';
$parent. Append (' <span class= ' formtips onError ' > ' +errormsg+ ' </span> ');
}else{
var okmsg = ' input correct. ';
$parent. Append (' <span class= ' formtips onsuccess ' > ' +okmsg+ ' </span> ');
}
}
//Verify Mail
if ($ (this). is (' #email ')) {
if (this.value== "" | | (this.value!= "" &&!/[email protected]+\. [A-za-z] {2,4}$/.test (this.value)) {
var errormsg = ' Please enter the correct e-mail address. ';
$parent. Append (' <span class= ' formtips onError ' > ' +errormsg+ ' </span> ');
}else{
var okmsg = ' input correct. ';
$parent. Append (' <span class= ' formtips onsuccess ' > ' +okmsg+ ' </span> ');
}
}
}). KeyUp (function () {
$ (this). Triggerhandler ("blur");
}). focus (function () {
$ (this). Triggerhandler ("blur");
});//end Blur


//Submit, Final verification
$ (' #send '). Click (function () {
$ ("form:input.required"). Trigger (' blur ');
var numerror = $ (' form. OnError '). length;
if (numerror) {
return false;
}
Alert ("Registration is successful, password has been sent to your mailbox, please check.");
});

  //reset
  $ (' #res '). Click (function () {
      $ (". Formtips"). Remove ();  
  });
})
//]]>
</SCRIPT>
<body>

<form method= "POST" action= "" >
<div class= "int" >
<label for= "username" > User name:</label>
<input type= "text" id= "username" class= "required"/>
</div>
<div class= "int" >
<label for= "Email" > Email:</label>
<input type= "text" id= "email" class= "required"/>
</div>
<div class= "int" >
<label for= "Personinfo" > Profile:</label>
<input type= "text" id= "Personinfo"/>
</div>
<div class= "Sub" >
<input type= "Submit" value= "commit" id= "send"/><input type= "reset" id= "res"/>
</div>
</form>

</body>

An example of a simple form validation in 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.