Use $. Ajax in jquery for instant Verification

Source: Internet
Author: User

Use jquery and the general handler to instantly verify that the student ID entered by the user is repeated. When the cursor leaves the input box, a prompt is displayed.

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "addstudent. aspx. cs" inherits = "addstudent" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
<Style type = "text/CSS">
. Clsshow
{
Font-size: 13px;
Border: solid 1px # cc3300;
Padding: 2px;
Display: none;
Margin-bottom: 5px;
Background-color: # ffe0a3;
}

</Style>
<SCRIPT type = "text/JavaScript" src = "scripts/jquery-1.4.2.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
$ (Function (){
$ ("# Btnsave"). Click (function (){
If ($ (". clsshow" example .html (). tostring ()! = "") // If a prompt message exists, the form cannot be submitted.
Return false;
Else
Return true;
});
$ ("# Txtnum"). Focus (); // enter the focus
$ ("# Txtnum"). keydown (function (event ){
If (event. Which = "13") {// enter the Enter key, move the cursor to the password box
$ ("# Txtname"). Focus ();
$ ("# Txtnum"). Trigger ("Blur ");
}
});
$ ("# Txtnum"). Blur (function (){
// Obtain the student ID
VaR strtxtname = encodeuri ($ ("# txtnum"). Val ());
// Start sending data
$. Ajax
({// Check whether the student ID is repeated
URL: "check. ashx ",
Type: "Post ",
// Send request data
Data: {txtnum: strtxtname },
Success: function (strvalue) {// data returned after Successful Logon
// Display the status based on the returned value
If (strvalue = "true") {// note that it is true, not true
$ (". Clsshow" ).css ("display", "inline ");
$ (". Clsshow" Student .html ("student ID already exists. Please modify it! ");
}
Else {
$ (". Clsshow"). Hide (); // Changes the display attribute to none.
$ (". Clsshow" developer.html ("");
}
}
})
})
})
</SCRIPT>

</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>

Student ID: <asp: textbox
Id = "txtnum" runat = "server"> </ASP: textbox>
<Asp: requiredfieldvalidator id = "requiredfieldvalidator1" runat = "server"
Controltovalidate = "txtnum" errormessage = "cannot be blank"> </ASP: requiredfieldvalidator>
<Div class = "clsshow"> </div>
<Br/>
Name: <asp: textbox id = "txtname" runat = "server"> </ASP: textbox>
<Asp: requiredfieldvalidator id = "requiredfieldvalidator2" runat = "server"
Controltovalidate = "txtname" errormessage = "cannot be blank"> </ASP: requiredfieldvalidator>
<Br/>
Mathematics: <asp: textbox
Id = "txtmath" runat = "server"> </ASP: textbox>
<Asp: requiredfieldvalidator id = "requiredfieldvalidator3" runat = "server"
Controltovalidate = "txtmath" errormessage = "cannot be blank"> </ASP: requiredfieldvalidator>
<Asp: rangevalidator id = "rangevalidator1" runat = "server"
Controltovalidate = "txtmath" errormessage = "scores between 0 and 100" maximumvalue ="
Minimumvalue = "0" type = "integer"> </ASP: rangevalidator>
<Br/>
English: <asp: textbox id = "txtenglish" runat = "server"> </ASP: textbox>
<Asp: requiredfieldvalidator id = "requiredfieldvalidator4" runat = "server"
Controltovalidate = "txtenglish" errormessage = "cannot be blank"> </ASP: requiredfieldvalidator>
<Asp: rangevalidator id = "rangevalidator2" runat = "server"
Controltovalidate = "txtenglish" errormessage = "scores between 0 and 100" maximumvalue ="
Minimumvalue = "0" type = "integer"> </ASP: rangevalidator>
<Br/>
Language: <asp: textbox id = "txtchinese" runat = "server"> </ASP: textbox>
<Asp: requiredfieldvalidator id = "requiredfieldvalidator5" runat = "server"
Controltovalidate = "txtchinese" errormessage = "cannot be blank"> </ASP: requiredfieldvalidator>
<Asp: rangevalidator id = "rangevalidator3" runat = "server"
Controltovalidate = "txtchinese" errormessage = "scores between 0 and 100" maximumvalue ="
Minimumvalue = "0" type = "integer"> </ASP: rangevalidator>
<Br/>
<Asp: button id = "btnsave" runat = "server" text = "save" onclick = "btnsave_click"/>
<Asp: button id = "btnback" runat = "server" text = "return" causesvalidation = "false"
Onclick = "btnback_click"/>
<Asp: Label id = "lblmsg" runat = "server"> </ASP: Label>
</Div>
</Form>
</Body>
</Html>

General handler check. ashx code:

<% @ Webhandler Language = "C #" class = "check" %>

Using system;
Using system. Web;

Public class check: ihttphandler {

Public void processrequest (httpcontext context ){
Context. response. contenttype = "text/plain ";
String num = context. request ["txtnum"]. tostring ();
Bool result = false;
If (num = "12") // No database is accessed to simplify the code. The actual project should query the database.
{
Result = true;
}
Context. response. Write (result );
}
Public bool isreusable {
Get {
Return false;
}
}

}

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.