A form validation class that you wrote yourself

Source: Internet
Author: User

Form Validation Class Usage Description:

myvalidation Class Usage Description: The current implementation function: Form form of non-null validation and mobile phone number validation default pop-up prompt box, the content of the prompt box is preferred: input corresponds to lable text+"cannot be empty"second choice: the placeholder field of input is therefore guaranteed at least one of the above is the intended use of the warning content:1, the JSP page introduces the Myvalidation.js file2, delete all JSP page onclick= of the original submit button"xxx.submit (); event3, Form form field is not empty: corresponding field plus class:required4, Form form fields are phone number format, corresponding field plus class:phonenum5If it is through the type= inside the form formsubmit button, after step 1, no other changes required6, if you raise the price by using the Click event of a button outside the form form, after step 1, you also need to add the Class:form-submit;data-form value for the button to the Name property of the corresponding form

Form Validation class source code

function IsNull (domelement) {if(domelement==NULL|| domelement==undefined| | (Domelement.trim&&domelement.trim () = ="")){        return true; }Else{        return false; }}function isphonenum (num) {if(IsNull (num)) {return false; }    //pattern=/(^[0-9]{3,4}\-[0-9]{3,8}$) | (^[0-9]{3,8}$) | (^\ ([0-9]{3,4}\) [0-9]{3,8}$] | (^0{0,1}13[0-9]{9}$)/;    varpattern=/^1\d{Ten}$/; if(Pattern.test (num)) {return true; }Else{        return false; }} //validates a single table, if the field contains the required class, the field cannot be empty, and if the Phonenum class is included, you need a phone numberfunction Isformvalid (formelement) {if(IsNull (formelement)) {return false; }     for(varI=0; i<formelement.elements.length;i++){        vartemp=Formelement.elements[i]; varContent=Temp.value; varClasstext=temp.getattribute ("class"); if(IsNull (Classtext)) {Continue; }        if(Classtext.indexof ("Required")!=-1){            if(IsNull (content)) {varPreviousnode=temp.previousSibling.previousSibling; //start by label to determine what kind of pop-up                if(!isnull (Previousnode) && (previousnode.nodename=="LABEL"|| prevousnode.nodename=="label") {alert (previousNode.firstChild.data+"can't be empty!"); return false; }Else{                    varPlaceholdertext=temp.getattribute ("placeholder"); if(!isNull (Placeholdertext))                        {alert (placeholdertext); return false; }                }                             }        }Else{            if(Classtext.indexof ("Phonenum")!=-1){                if(!Isphonenum (Temp.value)) {Alert ("phone number format is not correct!"); return false; }        }                    }    }    //otherwise pass validation//formelement.submit ();    return true;}//detects all forms on the page, which is suitable for submitting types through the Submit button within a form//If the submit event is triggered by a button other than the form, this method is not monitoredfunction Checkallpageform () {varforms=document.forms;  for(varI=0; i<forms.length;i++){        vartemp=Forms[i]; $ (temp). Submit (Function (Event){            if(Isformvalid (temp)) {temp.submit (); }Else{                if(Event. Preventdefault) {                    Event. Preventdefault (); }Else{                    Event. returnvalue=false;            }                                             }        }); }}//if you are submitting through a button outside the form form, you need to listen for the Click event of the form//Uniform Rules, add a uniform class Form-submit for such buttons, and add the Data-form field to the Name property of the form you want to submitfunction Checkpageformsubmitbybutton () {$btn _submit=$(". Form-submit"); if($btn _submit.length>0) {$btn _submit.click (function (Event){            varForm_name= This. getattribute ("Data-form"); if(!isNull (Form_name)) {                varform=Document.forms[form_name]; if(Isformvalid (form)) {//alert (isformvalid);Form.submit (); }Else{                    if(Event. Preventdefault) {                        Event. Preventdefault (); }Else{                        Event. returnvalue=false;    }                }            }                    });    }}$ (function () {checkallpageform ();  Checkpageformsubmitbybutton ();}); 

A form validation class that you wrote yourself

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.