Ajax asynchronous validation form simple instance in jquery

Source: Internet
Author: User

$.get and $.post methods, the first argument is the address, the second is the passed value, and the third is the successful callback function
Post method:

The code is as follows Copy Code

function Emailb () {
var email = $ ("#email"). Val ();
if (email== "") {
$ ("#inf_email"). ReplaceWith (' <span id= "Inf_email" >
<font color= "Red" >email address cannot be empty </font></span> ');
}
else{
$.post ("/index.php/index/checkemail2", {"email": Email, "randnum": math.random () + "},function

(RES) {
if (res== ' 1 ') {
$ ("#inf_email"). ReplaceWith (' <span id= "Inf_email" >
<font color= "Green" > can use </font></span> ');
}
else{
$ ("#inf_email"). ReplaceWith (' <span id= "Inf_email" >
<font color= "Red" > ' +res+ ' </font></span> ');
}
});
}
}

Get method:

The code is as follows Copy Code

var p = new Prompts ();
$ (' #username '). blur (function () {
var username = $ (' #username '). Val (); Get form values
if (username = = "") {
P.P (' #user_prompt ', ' P1 ', ' P2 p3 ', ' uppercase and lowercase letters, Chinese characters, numbers, underscores 3-12 bytes in length to

Inside '); C_user = ';
}else if (Username.length < 3) {
P.P (' #user_prompt ', ' P2 ', ' P1 p3 ', ' Username length error! ');
C_user = ';
}else if (!validate.username (username)) {
P.P (' #user_prompt ', ' P2 ', ' P1 p3 ', ' this username is illegal. ');
C_user = ';
}else{
To determine if the user name exists Ajax based on jquery
$.get ("chk_username.php", {username:$ (' #username '). Val ()
},function (data,textstatus) {
if (Data.indexof (' true ')!=-1) {
P.P (' #user_prompt ', ' P3 ', ' p1 p2 ', ' Congratulations, you can register with this username! ');
C_user = ' yes ';
}else{
P.P (' #user_prompt ', ' P2 ', ' P1 P3 ', ' The username has been registered, please choose another user name. ');
C_user = ';
}
})
}
ChkReg ()//Every time the mouse left to verify.
function ChkReg () {
if (C_user = = ' Yes ' && c_pwd1 = ' yes ' && c_pwd2 = ' yes ' && c_email = = ' Yes ' && c_checkcod E

= = ' Yes ') {
Flag = true;
}else{flag = false;}
}

Ajax methods

The code is as follows Copy Code

<form action= "/loupan/newask.php" method= "post" id= "SubmitForm" name= "SubmitForm" ><tr>
<td><table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" >
<tr>
<TD width= "6%" > Mobile phone:</td>
<TD width= "21%" ><input name= "Mo" type= "text" class= "INP" id= "Mo" size= "17"

/></td>
<TD width= "8%" align= "right" > Mailbox:</td>
<TD width= "21%" ><input name= "Email" type= "text" class= "INP" id= "email" size= "17"

/></td>
<TD width= "44%" ><span class= "Gray" > Leave your mobile number, there is the latest news we will send to your phone

</span></td>
</tr>
</table></td>
</tr>
<tr>
<TD height= "align=" "Left" >
<textarea name= "bak" cols= "rows=" 5 "class=" Lab "id=" BAK1 "></textarea></td>
</tr>
<tr>
<td><input name= "button" type= "button" class= "but" id= "Buttonajax" value= "Submit Message"

/>
<span class= "Gray" > Questions, will have a professional property consultant or home network friends to help you answer

</span></td>
</tr>
<input name= "lid" type= "hidden" value= "5976908"/>
<input name= "Cityid" type= "hidden" value= "1208"/>
<input name= "Cityareaid" type= "hidden" value= "1209"/>
<input name= "Fanginfo" type= "hidden" value= "Yuansi Square"/>
</form>

$ (' #buttonajax '). Click (function () {
Alert ($ (this). Val ());
Checkem ();
var postdata = $ (' #submitform '). Serialize ();
alert (postdata);
$.ajax ({
Type: "POST",
URL: "/xxxx.php",
Data:postdata,
Success:function (msg) {
Alert (msg);
if (msg==1) {
Alert (' Your question has been submitted successfully! ');
$ (' #bak1 '). Val (');
$ (' #mo '). Val (');

}else if (msg==-1) {
Alert (' Your problem submission failed, please fill in the submission content carefully! ');

}else if (msg==-2) {
Alert (' Please enter the correct phone number! ');
}
else if (msg==-3) {
Alert (' Sorry, there are illegal characters in the content you submitted! ');
}
else if (msg==-4)
{
Alert (' Sorry, you have submitted this question, please do not repeat the submission! ');
}
Else
{
Alert (' Sorry, System busy please try again later! ');
}
}
});
})

Related Article

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.