Ajax Form Validation Implementation code

Source: Internet
Author: User
Tags trim

Compatible: Opera 9.6 + Chrome 2.0 + FF 3 + IE 6


effect: Verify
while entering one side

Image


Environment: Ruby 1.8.6 + rails 2.1.0 + Windows


Core code:


HTML:


browser disables JavaScript when displaying a message:


copy code code as follows:


<noscript>


<div style= "color:red" > your browser does not support JavaScript, some features cannot be used </div>


</noscript>


when the browser disables JS, the Submit button is not available to implement client-side validation!


copy code code as follows:


<div id= "Js_support" style= "Display:none" >


<p id= "Psubmit" ><input type= "Submit" Name= "Registerform" value= "registered" onclick= "return SubmitForm ();" /></p>


</div>


<script type= "Text/javascript" >


$ ("Js_support"). Style.display = "block";


</script>


Listening for form fields via JavaScript onfocus and onkeyup:


<input type= "text" class= textbox "onfocus=" Checklogin (this); "onkeyup=" checklogin (this); "style=" width : 120px; "/>


Ajax Automatic Verification: the use of a bp.js, is already encapsulated XMLHttpRequest object, can be downloaded to my project below


copy code code 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, through the render:p artial return data to the client, of course, can be any platform under, such as the Java platform can use the servlet out.println (...) Back to


copy code code as follows:


def response_validate


if Params[:login]


name = Params[:login]


puts name


if name = = "AAAAAA"


@value = True


render:p artial => ' Show_info '


Else


@value = False


render:p artial => ' Show_info '


End


End


End

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.