Asp.net (JQuery) Form Verification

Source: Internet
Author: User
/*************************************** ******
Title: registration page ajax Form Verification
Author: zyr
Datetime: 2011-10-7
**************************************** *****/

$ (Document). ready (function (){
$ ('# Tb_name'). blur (function (){
CheckName (this );

});
$ ('# Tb_password'). blur (function (){
CheckPwd (this );

});
$ ('# Tb_confirmpassword'). blur (function (){
CheckConfirmPwd (this );

});
$ ('# Tb_email'). blur (function (){
CheckEmail (this );

});
});
/*************************************** **************************************** *
* Function name: resultremain
* Function Description: changes the form prompt status based on the passed value.
* Parameter: id: The span id of the form; str: prompt text; cssclassname: Change the css style class name of the prompt color

**************************************** **************************************** */

Function resultremain (id, stype, str, cssclassname ){
Switch (stype ){
// Default
Case "normal ":
{

$ ("# Span _" + id). addClass (cssclassname );
Break;
}
// Correct
Case "right ":
{
$ ("# Span _" + id).html (str );
$ ("# Span _" + id). addClass (cssclassname );
Break;
}
// Error
Case "error ":
{
$ ("# Span _" + id). addClass (cssclassname );
$ ("# Span _" + id).html (str );
Break;
}
// Obtain the focus
Case "focus ":
{
$ ("# Span _" + id).html (str );
$ ("# Span _" + id). addClass (cssclassname );
}
Default:
Break;
}
}
/*************************************** **************************************** ***
* Function name: checkName (o)
* Function Description: checks whether the user name is valid. No ajax check is performed for the moment.
* Parameter: o: form object;
* Call method: form name. attributes ["onblur"] = "return checkName (this);"; // (asp.net)
**************************************** **************************************** ***/
Function checkName (o ){

Var u = o. value;
Var id = o. id;
If (u ){
If (u. length <4 | u. length> 20 ){

Resultremain (id, "error", " the username length must be between 4 and 20! "," Formerr ");
Return false;
}
} Else {
Resultremain (id, "error", " the user name cannot be blank! "," Formerr ");
Return false;
}
Var pattern =/^ ([a-za-z0-9] | [\ u4e00-\ ufa2d]) + $ /;
If (! Pattern. test (u )){
Resultremain (id, "error", " the user name contains invalid characters! "," Formerr ");
Return false;
}
Else {
// Determine whether the user name is registered
Return $. get ("AjaxUserIsExist. ashx ",{
"UserName": u
}, Function (msg ){
If (msg = "false "){
Resultremain (id, "error", " <font color = 'red'> the user name already exists! </Font> "," formerr "); // the user name already exists.
Return false;
}
Else {
Resultremain (id, "right", "Return true;
}
}

);
}

// If (u = o1.value & u! = ""){
// Regstyle (o. id, "right", "this user name can be used ");
// Return false;
//}
// Var flag = loadxml (hostname + "/callpage/query. aspx? Op = queryuid & s = "+ o. value );
// If (flag = 'false '){
// Regstyle (o. id, "right", "this user name can be used ");
// O1.value = u;
//} Else {
// Regstyle (o. id, "error", "this user name has been registered ");
// Return false;
//}

}
/*************************************** **************************************** *
* Function name: checkPwd (o)
* Function Description: triggers password detection when the form loses focus.
* Parameter: o: form object
* Call method: form name. attributes ["onfocus"] = "return checkPwd (this);"; // (asp.net)
**************************************** **************************************** */
Function checkPwd (o ){
Var p = o. value;
If (p ){
// Test the password length, including the Chinese character password
Var len = 0;
Var ary = escape (p). match (/(% u [0123456789 abcdef] {4}) +/g );
If (ary ){
Len = unescape (ary. join (""). length * 2;
P = unescape (escape (str). replace (/(% u [0123456789 abcdef] {4}) +/g ,""));
Len + = p. length;
} Else {
Len = p. length;
}
If (len <6 | len> 20 ){

Resultremain (o. id, "error", " the password length must be between 6 and 20! "," Formerr ");
Return false;

} Else {

Resultremain (o. id, "right", "Return true;
}

} Else {
Resultremain (o. id, "error", " the password cannot be blank! "," Formerr ");
Return false;
}
};
/*************************************** **************************************** *
* Function name: checkConfirmPwd (o)
* Function Description: checks whether the password entered again matches
* Parameter: o: form object
* Call method: form name. attributes ["onfocus"] = "return checkConfirmPwd (this);"; // (asp.net)
**************************************** **************************************** */
Function checkConfirmPwd (o ){
Var pwd = $ ('# tb_password'). val ();
Var ConfirmPwd = o. value;
If (ConfirmPwd. length> 0 ){
If (pwd! = ConfirmPwd ){
Resultremain (o. id, "error", " inconsistent passwords! </Span> "," formerr ");
Return false;
}
Else {
Resultremain (o. id, "right", "Return true;
}
}
Else {
Resultremain (o. id, "error", " the password cannot be blank! "," Formerr ");
Return false;
}
}
/*************************************** **************************************** ***
* Function name: checkEmail (o)
* Function Description: Check whether email is valid.
* Parameter: o: form object;
* Call method: form name. attributes ["onblur"] = "return checkEmail (this);"; // (asp.net)
**************************************** **************************************** ***/
Function checkEmail (o ){
Var em = o. value;
If (em ){
Var pattern =/^ (\ w | _ | \.) + @ (\ w | _ | \-) + \.) + (\ w) {2, 3} $ /;
If (! Pattern. test (em )){
Resultremain (o. id, "error", " the input of E-mail is invalid! "," Formerr ");
Return false;
}
Else {
Resultremain (o. id, "right", "Return true;
}
} Else {
Resultremain (o. id, "error", " E-mail cannot be blank! "," Formerr ");
Return false;
}

}
/*************************************** **************************************** ***
* Function name: RegForm ()
* Function Description: prevents submission when a form is incorrect or incorrect.
* Call method: from tag onsubmit = "return RegForm ();" or ImageButton (submit button) OnClientClick = "return RegForm ();"; // (asp.net)
**************************************** **************************************** ***/
Function RegForm (){

Var falsestr = "";

If (! CheckName ($ ('# tb_name') [0]) {
Falsestr = "false ";
}
If (! CheckPwd ($ ('# tb_password') [0]) {
Falsestr = "false ";
}
If (! CheckConfirmPwd ($ ('# tb_confirmpassword') [0]) {
Falsestr = "false ";
}
If (! CheckEmail ($ ('# tb_email') [0]) {
Falsestr = "false ";
}
If (falsestr = "false "){

// Alert ('the form contains invalid characters ')
// Modify ('{lab_error'{.html ("Please modify the error message and submit it again! ");
// $ ('# Btn_register'). preventDefault ();
// $ ('# Btn_register'). bind ('submit ', function (){
// Return false;
//})
// $ ('# Btn_register'). submit (function (){
// Return false;
//});
// $ ('# Btn_register'). attr ("disabled", true );
Return false;
} Else {
// $ ('# Btn_register'). attr ("disabled", false );
Return true;

}

}

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.