The practice of form validation

Source: Internet
Author: User

functionCheckform (from) {varFrom_text = $ (from). Find (' Input[type=text],input[type=password] '), isOK=true, Prompt=[{Type:"User", Reg:/^[\u4e00-\u9fa5]{1,10}[.] {0,1} [\u4e00-\u9fa5] {1,10}$/, msg:"Please fill in the user name", Tipe:"User name cannot be empty"},{Type:"PSW", Reg:/^[a-za-z0-9_]\w{5,17}$/, msg:"Password length is 6-18-character number", Tipe:"Password cannot be empty"},{Type:"Name", Reg:/^[\u4e00-\u9fa5]{1,10}[.] {0,1} [\u4e00-\u9fa5] {1,10}$/, msg:"Please fill in the correct contact", Tipe:"Contact cannot be empty"},{Type:"Phone", Reg:/^ (0|86|17951)? (13[0-9]|15[012356789]|17[678]|18[0-9]|14[57]) [0-9] {8}$/, msg:"Please fill in the correct phone number", Tipe:"Phone number cannot be empty"        }    ]; From_text.each (function(i) {varval = $ ( This). Val (), name= $( This). attr ("name"), Tip= $( This). attr ("MyAttr"), Num=prompt.length; if(tip) {if(val = = "") {alert (Tip+ "cannot be empty"); isOK=false; return false            };  for(varj = 0; J < num; J + +) {                if(Name = =Prompt[j]. Type) {if(!Prompt[j].reg.test (val))                        {alert (prompt[j].msg); isOK=false; return false;                };            };         }; }      })    returnisOK;}

This is the form verification that you wrote before, the disadvantage or very much need to rely on the Name property to do the validation lookup (there is no validation of the Name property is not verified), the empty hint is written in the myttr attribute of input, each form of special hints written in Prompt inside, Rely on loops to find the corresponding name for verification

If I write a lot of validation types in the prompt, then it's not going to be recycled many times (the efficiency is too low!!!) Sweat!!! ), and the worst point if there are 2 mobile phones in a page to verify and to submit a different name then how to do that same verification is not the same name to write 2 (this is not science!!! )

Hey, yo! Recently entered the new company found that colleagues write a package of what is better than I'm too much more than I posted out contrast one ;

(function(Win, Lib) {varPatientinfo ={youngyear:14, Realnamereg:/^ ([a-z]| [a-z]| [\U2E80-\U9FFF]) {1,8}/, Cardnumreg:/^ ([a-z]| [a-z]|\d) {1,18}/, Idcardreg:/^\D{17} ([a-z]|[ a-z]|\d) {1}/, Phonereg:/^\d{11}$/, verify:function(el, Verifyreg, Verifyalert, ignorenull) {varVerifytext = El.val (). Replace (/\t+/g, ""). Replace (/\n+/g, ""). Replace (/\r+/g, ""). Replace (/\s+/g, ""); if(Verifytext = = "") {                if(ignorenull) {return false; } lib.hkdialog ({type:3, Surecallback:function() {}, Content:verifyalert+ ' cannot be empty ', Suretxt:' I know. '                }); return true; }            if(!verifyreg.test (Verifytext)) {Lib.hkdialog ({type:3, Surecallback:function() {}, Content:' Please enter the correct ' +Verifyalert, Suretxt:' I know. '                }); return true; }            return false; } },
This is the perfect dividing line----------------------------------------------------------------------------------------------
Here is the call

Submitfun:function () {var = this;
$ ('. P-save '). On (' Tap ', function (e) {
var pactive = $ ('. P-active ');
Name
var name = Pactive.find ('. P-name ');
if (that.verify (name, That.realnamereg, ' real name ')) {return}
The verification is done true is not through
There's something else down there that doesn't matter.
}
Document.addeventlistener (function  (e) {        patientinfo.init ();     false );}) (Window, window[' lib ') | | (window[' lib '] = {}));


First of all, this code is not all (there are some other dependent components), Lib stands for the other common calls we encapsulated method library (or component called), like Lib.hkdialog refers to the call of a similar pop-up hint of the component (simply said is alert) Wow! This kind of package is very good AH!!! I get new skills again! Let's talk about the implementation of this component!

The top of those regular actually and I wrote that is a truth is to be used to verify the method, there are 3 parameters one is to verify the value, one is the validation of the bar, one is the prompt, there is a can be empty options

Then go to the space or something, first Judge Ignorenull If it is true can be empty (however I just saw here when I think if I want to not be empty and do not verify how to do Oh!!! I'm really a pig! This situation directly to the end of the space and then determine whether it is empty or not! Shame!!! )

The verification is done. True is not passed (why?) I am accustomed to return is fales is not through, anti-reverse! You can write one less! , I used this for a long time to find a problem later to see the code to find it)
This verify verify that there are few things written in it! But it's more flexible to use,
It's less than mine. Cycle efficiency is better,
The validation content and the regular pass in, the prompt language passes in basically can! Use it more convenient and flexible, use it later! (but I still want to turn him back to true and false in turn!) Hey! )

The practice of form validation

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.