JS Regular expression for format check

Source: Internet
Author: User

Today, I did a JS regular expression exercise, using regular expressions for registration Information format Verification, the registration information interface is as follows:


Format requirements:

1, the number of items can not be empty, must be a pure number, can not be duplicated with the database, regular expression /^\d+$/g;

2, the name item cannot be empty;

3, password can not be empty and no space to determine the space text.split (""). Length! = 1, the security level is divided into 3 levels, if all letters or numbers are 1 levels, regular Expressions (Text.search (/^[a-za-z]+$/g)! = 1 ) || (Text.search (/^[0-9]+$/g)! =-1, if only punctuation or punctuation is present at the same time as a letter or number 2, the regular expression text.search (/^[a-za-z0-9]+$/g)! = 1, if the letter, Numbers and punctuation appear at the same time as Level 3;

4, confirm the password entry requirements and password entries to fill in the same password;

5. The grade item cannot be empty, and the format must be level 20**, regular expression text.search (/^\u7b2c{1}20\d+\u7ea7{1}$/)! = 1;

6, the professional items can not be empty, and can only start with Chinese characters, the end can be Chinese characters or the letter regular expression Text.search (/^[\u4e00-\u9fa5]+[a-za-z]*$/g)! =-1;

7, class items can not be empty and the format of the Professional + class is similar to the telecommunications 1001, regular expression text.search (/^[\u4e00-\u9fa5]+\d{4}$/)! = 1;

8, the mobile phone item can be empty, the format is (+86) 1********* Regular expression text.search (/^ (\+86)? 1\d{10}$/)! =-1.

Operating effect:


Run the effect after the correct format is entered:



HTML code:

<!--register--><div style= "Display:block" ><div><span style= "font-weight:bold;" ><label> Register </label></span></div><div><span class= "Input_span" ><label class= "Text_label" > Study No. </label><label class= "Text_label" style= "color:red;" >*</label></span><span class= "Input_span" ><input type= "text" id= "StudentNum_input" onblur= "Checkregisterinfo (1)"/></span><span><label id= "Studentnumcheck_label" ></label></ Span></div><div><span class= "Input_span" ><label class= "Text_label" > Name </label> <label class= "Text_label" style= "color:red;" >*</label></span><span class= "Input_span" ><input type= "text" id= "Name_input" onblur= " Checkregisterinfo (2) "/></span><span><label id=" Namecheck_label "></label></span ></div><div><span class= "Input_span" ><label class= "Text_label" > Password </label> <label class= "Text_label" style= "color:red;" >*</label></span><span class= "Input_span" ><input type= "password" id= "Passwd_rigester_ Input "onblur=" Checkregisterinfo (3) "/></span><span><label id=" Passwdcheck_label "></label" ></span></div><div><span class= "Input_span" id= "confirmpasswd_span" ><label> Confirm password </label><label class= "Text_label" style= "color:red;" > *</label></span><span class= "Input_span" ><input type= "password" id= "ConfirmPasswd_ Input "onblur=" Checkregisterinfo (4) "/></span><span><label id=" Confirmpasswdcheck_label "> </label></span></div><div><span class= "Input_span" ><label class= "Text_label" > Grade </label><label class= "Text_label" style= "color:red;" >*</label></span><span class= "Input_span" ><input type= "text" id= "Grade_input" onblur= " Checkregisterinfo (5) "/></span><span> <label id= "Gradecheck_label" > Format: section 20** </label></span></div><div><span class= " Input_span "><label class=" Text_label "> Professional </label><label class=" Text_label "style=" color:Red; " >*</label></span><span class= "Input_span" ><input type= "text" id= "Major_input" onblur= " Checkregisterinfo (6) "/></span><span><label id=" Majorcheck_label "></label></span ></div><div><span class= "Input_span" ><label class= "Text_label" > Classes </label>< Label class= "Text_label" style= "color:red;" >*</label></span><span class= "Input_span" ><input type= "text" id= "Class_input" onblur= " Checkregisterinfo (7) "/></span><span><label id=" Classcheck_label "> Format: Telecom 1001</label> </span></div><div><span class= "Input_span" id= "Phone_span" ><label class= "Text_label" > Mobile </label></span><span class= "Input_span" ><input TypE= "text" id= "Phone_input" onblur= "Checkregisterinfo (8)"/></span><span><label id= "PhoneCheck_ Label "></label></span></div><div class=" Button_div "><span><input id=" Register_button "type=" button "onclick=" Summitregisterinfo () "value=" User Registration "/></span></div></div ><!--End Register--></div>


JS Authentication Source code:

/** function: Verify that the registration information is legal, call the * parameter when the <input> control loses focus: Num control number, indicates which control triggered the function * return value: If all is valid return true, otherwise give a response error prompt and return false* create    Person: lixam* creation time: 2013-9-14* Modified by: * Modified: */function checkregisterinfo (num) {var text; Switch (num) {///When the Submit button is clicked, check if the required item is empty, preventing direct click submit Button Case 0:if (document.getElementById ("Studentnum_inp UT "). Value = =" "| | document.getElementById ("Name_input"). Value = = "" | | document.getElementById ("Passwd_rigester_input"). Value = = "" | | document.getElementById ("Confirmpasswd_input"). Value = = "" | | document.getElementById ("Grade_input"). Value = = "" | | document.getElementById ("Major_input"). Value = = "" | |                document.getElementById ("Class_input"). Value = = "") {Alert ("Registration failed, key cannot be empty!");            return false;            } else return true;        Break Verification study number Case 1:text = Document.getelementbyId ("Studentnum_input"). Value;            var check = document.getElementById ("Studentnumcheck_label");                Verify if the null if (Text = = "") {Check.style.color = "red";            Check.innertext = "Study number item cannot be empty!";                }//Verify the format else if (Text.search (/^\d+$/g) = =-1) {Check.style.color = "red";            Check.innertext = "Study number should be a pure number!";                } else {//Verify the uniqueness of the number var xmlHttp = Createxmlhttp ();                Xmlhttp.open ("Get", "Ajax.aspx?met=rigesterinfo&data=" + Escape (Text), true);                Xmlhttp.send (NULL);                        Xmlhttp.onreadystatechange = function () {if (xmlhttp.readystate = = 4 & xmlhttp.status = = 200) { The server returns true to indicate that the study number is available if (xmlhttp.responsetext) {che                            Ck.style.color = "Yellow";             Check.innertext = "Congratulations, the study number is available!";           } else {check.style.color = "red";                        Check.innertext = "You have entered a number already exists, please re-enter!";        }}}} break;            Verify name Case 2:text = document.getElementById ("Name_input"). Value;            var check = document.getElementById ("Namecheck_label");                if (Text = = "") {Check.style.color = "red";            Check.innertext = "Name entry cannot be empty!";                } else {check.style.color = "yellow";            Check.innertext = "The name item is filled in correctly!";        } break;            Verify the password case 3:text = document.getElementById ("Passwd_rigester_input"). Value;            var check = document.getElementById ("Passwdcheck_label");                if (Text = = "") {Check.style.color = "red";            Check.innertext = "Password entry cannot be empty!"; }//PasswordThere can only be numbers, letters, and punctuation marks else if (Text.split (""). Length! = 1) {Check.style.color = "red";            Check.innertext = "There is no space in the password!"; } else {//Verify the security level of the password, pure or pure or pure punctuation is 1 levels, the letter + number is level 2, the letter or number is any one + punctuation 3-level if (Text.search (/^[ a-za-z]+$/g)! =-1) | | (Text.search (/^[0-9]+$/g)! =-1))                    {Check.style.color = "yellow";                Check.innertext = "Password security level is 1 level!";                    } else if (Text.search (/^[a-za-z0-9]+$/g)! =-1) {Check.style.color = "yellow";                    Check.innertext = "Password security level is 2 level!";                    } else {check.style.color = "yellow";                Check.innertext = "Password security level is 3 level!";        }} break;            Verify the confirmation password case 4:text = document.getElementById ("Confirmpasswd_input"). Value;    var check = document.getElementById ("Confirmpasswdcheck_label");        if (Text! = document.getElementById ("Passwd_rigester_input"). Value) {Check.style.color = "red";            Check.innertext = "Two times password input inconsistent!";                } else {check.style.color = "yellow";            Check.innertext = "Password confirmation is correct!";        } break;            Verify Grade Case 5:text = document.getElementById ("Grade_input"). Value;            var check = document.getElementById ("Gradecheck_label");                if (Text = = "") {Check.style.color = "red";            Check.innertext = "Grade entry cannot be empty!";                } else if (Text.search (/^\u7b2c{1}20\d+\u7ea7{1}$/)! =-1) {Check.style.color = "yellow";            Check.innertext = "Grade item is filled in correctly!";                } else {check.style.color = "red";            Check.innertext = "Grade Item format: Grade 20**!";        } break; Verify Professional Case 6:text = document.getElementById ("MajoR_input "). Value;            var check = document.getElementById ("Majorcheck_label");                if (Text = = "") {Check.style.color = "red";            Check.innertext = "Professional cannot be empty!";                } else if (Text.search (/^[\u4e00-\u9fa5]+[a-za-z]*$/g)! =-1) {Check.style.color = "yellow";            Check.innertext = "Professional item filled in correctly!";                } else {check.style.color = "red";            Check.innertext = "The professional item is not filled in correctly!";        } break;            Verify class Case 7:text = document.getElementById ("Class_input"). Value;            var check = document.getElementById ("Classcheck_label");                if (Text = = "") {Check.style.color = "red";            Check.innertext = "Class item cannot be empty!";                } else if (Text.search (/^[\u4e00-\u9fa5]+\d{4}$/)! =-1) {Check.style.color = "yellow";            Check.innertext = "Class item filled in correctly!"; } ELSE {Check.style.color = "red";            Check.innertext = "Class Item format: Telecom 1001!";        } break;            Verify phone Case 8:text = document.getElementById ("Phone_input"). Value;            var check = document.getElementById ("Phonecheck_label");            if (Text = = "") {break;                } else if (Text.search (/^ (\+86)? 1\d{10}$/)! =-1) {Check.style.color = "yellow";            Check.innertext = "The phone item is filled in correctly!";                } else {check.style.color = "red";            Check.innertext = "Phone item format is wrong!";    } break; }}

JS Regular expression for format 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.