Native JS (form) validation, can be used to learn from the ideas, applied to the work

Source: Internet
Author: User

I often use form validation at work, in the current company to do forms validation with the angular form component, for a pursuit of the front end, or application on the mobile side to write a form verification, the introduction of angular or jquery components, such as verification, will inevitably appear bloated, Preferably a native JS bar, light weight. Fortunately, until this lesson, plus the previous learning, slowly merge according to the requirements, applied to the work project ...

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

  

//Copyright Beijing Intelligent Society ©, All rights reservedvarJSON ={username:/^[a-z][a-z0-9_\-$]{5, to}$/I, Tel:/^(0[1-9]\d{1,2}-)? [1-9]\d{6,7}$/, Email:/^\[email protected][a-z0-9\-]+(\. [A-z] {2,6}){1,2}$/I, Age:/^(1[6-9]| [2-9]\d| -)$/};function checkform (id) {varOform =document.getElementById (ID); varAinput =Oform.children;  for(vari =0; i < ainput.length; i++){        varRe =Json[ainput[i].name]; if(re) {(function (re) {Ainput[i].onblur=function () {Checktext (Re, This);            };        }) (re); }} function Checktext (re,otext) {if(Re.test (Otext.value)) {Otext.classname="OK"; return true; } Else{otext.classname="Error"; return false; }} oform.onsubmit=function () {varBOk =true;  for(vari =0; i < ainput.length; i++){            varRe =Json[ainput[i].name]; if(re) {if(Checktext (re,ainput[i]) = =false) {bOk=false; }            }        }        if(BOk = =false){            return false; }    };}
View Code

Native JS (form) validation, can be used to learn from the ideas, applied to the work

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.