"JQuery" uses the serialization of a form to take a form based on name, to validate a form

Source: Internet
Author: User

There are many ways to verify the form in the foreground, the specific idea is that you first want to verify the value of the form to check out, and then the script to judge, and finally according to the results of the alarm, and then give onsubmit= "return XX ()" That function, return True and False Boolean value. Throughout the process, it is critical to take the form's validation items out. In the "JavaScript" form of instant verification, do not successfully do not allow the submission (click Open link) in the use of ID to take the properties of each form item, this is the most basic, but if you need to take the name of the method to remove the form of the item? At this point, you can take advantage of the serialization of jquery to remove individual items of the form according to name.

I. BASIC OBJECTIVES

Using the serialization of jquery, the various items of the form are taken out according to name to do form validation. Such as. If the user finishes entering the form correctly, all information entered by the user is popped up.



Second, HTML layout

Paste the HTML layout code for this form first. Very simple. Just please note that I just gave each table item the Name property, no id attribute. The following jquery script is entirely based on name.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Third, jquery script

1, the entire script at the beginning, after getting the form form, first remove all the values in the form by the Serialize () method, the return value of the Serialize () method is a string to get the way to commit, you can see in the above, also. After that, first use the split () method, according to & to divide the array rawparamarray, each element in the arrays Rawparamarray is Paramname=paramvalue form, is user=sss this form. After the array rawparamarray to traverse, traverse to rawparamarray each item, again according to = to divide the arrays ParamArray, then the resulting array ParamArray the first item is the property name, the second item is the property value. Then, how to judge how to judge. If a judgment failure is judged, the Boolean value of this item will evolve to false.

2, for the single box radio, check box checkbox, if you do not get the property name of an item, the property value means that this radio box, check box is not selected at all!

3, for the password entry, because to determine whether and after the password entry is consistent, so to take the next password entry to judge, if you feel bored, you can determine the password entry, set the ID, directly with the ID to fetch is also no problem.

<script>function submitpreprocessing (obj) {var inputtextok=true;var passwordlength=false;var passwordCheck= False;var radioselected=false;var checkboxselected=false;var rawparamarray=$ (obj). Serialize (). Split ("&"); for ( var i=0;i<rawparamarray.length;i++) {var paramarray=rawparamarray[i].split ("="); if (paramarray[0]== "user") | | (paramarray[0]== "pwd") | | (paramarray[0]== "PS")) {if (paramarray[1]== "") {Inputtextok=false;}} if ((paramarray[0]== "pwd")) {if (paramarray[1].length>5) {passwordlength=true;} if (paramarray[1]+ "= = (Rawparamarray[i+1].split (" = ") [1]+" ")] {passwordcheck=true;}} if ((paramarray[0]== "R")) {radioselected=true;} if ((paramarray[0]== "C1")) {checkboxselected=true;}} if (!inputtextok) {alert ("username, password, comment any one is empty!") ");} if (!passwordlength) {alert ("Your password is less than 6 bits long! ");} if (!passwordcheck) {alert ("Two times input password is inconsistent!") ");} if (!radioselected) {alert ("Your Radio box has not been selected!") ");} if (!checkboxselected) {alert ("Your C1 check box has not been selected!") ");} if (inputtextok&&passwordlength&&passwordcheck&&radioselected&&checkboxselected{Alert ("Thank you for filling in"), alert (obj). serialize ()); return true;} Else{alert ("Your form is not ready yet!") "); return false;}} </script>


"JQuery" uses the serialization of a form to take a form based on name, to validate a form

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.