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 CodeNative JS (form) validation, can be used to learn from the ideas, applied to the work