Self-built JS code base (1)---Add user's Common validation

Source: Internet
Author: User


Self-built JS code base (1)---Add user's Common validation

We all know that there are many more mature JavaScript code libraries, such as: Jquery,prototype, etc. There are many proven, well-used functions. These good code libraries can improve our development efficiency, but we also encounter some common methods in the development process, and these may be more cumbersome to write, this time we have their own code base directly copy code can make you happy?

Today, a simple collation, in the project development process to add users of the system may use some of the common validation.

1, judge the user name can only be letters or numbers, and the length is 4~6 bit

1, the general method---judge the user name can only be letters or data, and the length of 4~6 bit if (!) Trim (useridfield.value). Length >=4&& trim (useridfield.value). Length <=6) {         alert ("User code can only be 4~6 bit!! ");         Useridfield.focus ();         return;} For (Var I=0;i<trim (useridfield.value). length; i++) {         Varc = Trim (useridfield.value). CharAt (i);         if (! ( (c >= ' 0 ' && C <= ' 9 ') | | (c >= ' a ' && c <= ' z ') | | (c >= ' A ' && C <= ' Z '))) {                   alert ("User code must be numbers and letters!"} ");                   Useridfield.focus ();                   return;}         }                  2, the regular expression---judge that the user name can only be letters or data, and the length is 4~6 bit var re = new RegExp (/^[a-za-z0-9]{4,6}$/); if (!re.test (Trim (useridfield.value))) {         alert ("User code must be a number or letter, only a 4~6 bit!") ");         Useridfield.focus ();         return;}

2, judge the contact number is the number (not empty)

var Contacttelfield =document.getelementbyid ("Contacttel");//Do not use the regular if (Trim (contacttelfield.value)! = "") {for         ( var i=0; I<trim (contacttelfield.value). length; i++) {                   Varc = Trim (contacttelfield.value). CharAt (i);                   if (! ( C >= ' 0 ' && C <= ' 9 ') {                            alert ("Phone number is not legal! ");                            Contacttelfield.focus ();                            return;}}}         Use regular if (Trim (contacttelfield.value)! = "") {         //Use regular         re.compile (/^[0-9]*$/);         if (!re.test (Trim (contacttelfield.value))) {                   alert ("Phone number is not legal!") ");                   Contacttelfield.focus ();                   return;         }       }

3, determine whether email contains @ (email is not empty, and @ no longer the end of the place)

var Emailfield =document.getelementbyid ("email"); if (Trim (emailfield.value). Length! = 0) {         Varemailvalue = Trim ( Emailfield.value);         if ((Emailvalue.indexof ("@") = = 0) | | (Emailvalue.indexof ("@") = = (emailvalue.length-1))) {                   alert ("The email address is incorrect! ");                   Emailfield.focus ();                   return;                                         }         if (Emailvalue.indexof ("@") < 0) {                   alert ("Email address is incorrect!) ");                   Emailfield.focus ();                   return;}                                         }


Once heard that a good programmer is not how much time you can write a thing, but can use the shortest time to make something, which requires that we have a good accumulation, have seen a lot of different code, and in need of time can be quickly applied to their own projects.

I think this has to start with writing your own code base.

Self-built JS code base (1)---Add user's Common 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.