Ajax Form Verification implementation code

Source: Internet
Author: User

Compatible with: opera 9.6 + chrome 2.0 + FF 3 + IE 6
Effect: verification is performed on the input side.
 
Environment: ruby 1.8.6 + rails 2.1.0 + windows
Core code:
Html:
The prompt message is displayed when the browser disables javascript:
Copy codeThe Code is as follows:
<Noscript>
<Div style = "color: red"> your browser does not support javascript and some functions cannot be used </div>
</Noscript>

When the browser disables JS, the submit button is unavailable for client verification!
Copy codeThe Code is as follows:
<Div id = "js_support" style = "display: none">
<P id = "pSubmit"> <input type = "submit" name = "registerform" value = "" onclick = "return submitform (); "/> </p>
</Div>
<Script type = "text/javascript">
$ ("Js_support"). style. display = "block ";
</Script>

Use onfocus and onkeyup in javascript to listen to form fields:
<Input type = "text" class = "textbox" onfocus = "checkLogin (this);" onkeyup = "checkLogin (this);" style = "width: 120px;"/>
Automatic ajax verification: a bp. js object is an encapsulated xmlhttprequest object, which can be downloaded to my project below
Copy codeThe Code is as follows:
Function checkLogin (node ){
ClearError ();
If (bp. String. trim (node. value )! = ""){
Var url = "/test/response_validate ";
Var params = [];
Params ["command"] = "logincheck ";
Params ["login"] = encodeURI (bp. String. trim (node. value ));
Var v = validatePath ("login", params ["login"]);
If (v = ""){
Bp. Io. XhrGet ({
Url: url,
MimeType: "text/json ",
Params: params,
Load: function (type, data, e ){
If (data = true ){
LoginError = document. getElementById ("valstatus"). innerHTML = 'login name already exists ';
Document. getElementById ("loginValidate"). className = "icon_cross ";
Node. className = "input_error ";
LoginValid = false;
} Else {
LoginError = "";
Document. getElementById ("loginValidate"). className = "icon_ OK ";
Node. className = "textbox ";
LoginValid = true;
}
},
Error: function (type, data, e ){}
});
} Else {
LoginError = document. getElementById ("valstatus"). innerHTML = v;
Document. getElementById ("loginValidate"). className = "icon_cross ";
Node. className = "input_error ";
LoginValid = false
}
} Else {
LoginError = validatePath ("login ","");
Document. getElementById ("loginValidate"). className = "";
Node. className = "textbox ";
}
}

Background:
This is the background of rails. It returns data to the client through render: partial. Of course, it can be on any platform. For example, the servlet out can be used on the java platform. println (...) return
Copy codeThe Code is as follows:
Def response_validate
If params [: login]
Name = params [: login]
Puts name
If name = "aaaaaa"
@ Value = true
Render: partial => 'show _ info'
Else
@ Value = false
Render: partial => 'show _ info'
End
End
End

Download the complete source code:
Jsvalidate.rar

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.