Regular expressions are patterns used to match character combinations in strings. In JavaScript, regular expressions are also objects. These patterns are used for the exec and test methods of RegExp, as well as the match, replace, search and split methods of String. This chapter introduces JavaScript regular expressions.
1. Verify whether the account is legal
Verification rules: composed of letters, numbers, and underscores, beginning with letters, 4-16 digits.
function checkUser(str){ var re = /^[a-zA-z]\w{3,15}$/; if((str)){ alert("correct"); }else{ alert("mistake"); } } checkUser("");//Call
2. Mobile phone number
Verification rules: 11 digits, starting with 1.
function checkMobile(str) { var re = /^1\d{10}$/ if ((str)){ alert("correct"); }else{ alert("mistake"); } } checkMobile('13800138000'); //Call
3. Phone number
Verification rules: Area code + number, area code starts with 0, 3 or 4 digits, the number consists of 7 or 8 digits, there can be no connector between the area code and the number, or it can be connected "-"
function checkPhone(str){ var re = /^0\d{2,3}-?\d{7,8}$/; if((str)){ alert("correct"); }else{ alert("mistake"); } } checkPhone("09557777777");//Call
4. Email
Verification rules: Let’s divide the email address into “Part 1 @Part 2”. The first part: consists of letters, numbers, underscores, short-line "-", and dot "."; the second part: is a domain name, and the domain name is composed of letters, numbers, short-line "-", and domain name suffixes, while the domain name suffixes are generally .xxx or . The domain name suffixes in area 1 are generally 2-4 digits, such as cn, com, and net. Some domain names are now greater than 4 digits.
function checkEmail(str){ var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/ if((str)){ alert("correct"); }else{ alert("mistake"); } } checkEmail("contact@");//Call
Summarize
The above is the Javascript regular expression introduced by the editor to verify the legality of the account, mobile number, phone number and email address. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!