1. Check if the email address is empty:
var mail=$("#email").val();
if(mail==""){
//检测Email是否为空
Alert("邮箱不能为空");
Return false;
}
2. Check whether the mailbox contains "@"
if(mail.indexOf("@")==-1){
Alter("邮箱格式必须含@");
Return false;
}
The method of submission is submit
$("form").submit(function(){
//校验
//需要返回值
if(!checkEmail()){
return false;
}
if(!checkPass()){
return false;
}
return true;
})
//除了提交的时候进行校验还有输入框失去焦点的时候也需要进行校验
$("input[calss=email]").blur(checkEmail);
3. Check whether the mailbox contains ".";
if(mail.indexOf(".")==-1"){
Alert("邮箱必须包含有.");
Return false;
}
4. Check whether the account number or password input contains numbers:
Var user=$("#user").val();
For(var i=0; i<user.length; i++){
Var j=user.substring(j,j+1);
if(isNaN(j)==false){
Alter("不能包含数字");
Return false;
}
}
{n} | number of matches |
---|---|
{n,m} | matches n to m |
{n,} | at least n |
+ | One or more times in front of one zo+ zo zoo |
。 | o times or multiple times zo* zo z zoo |
? | o 或1 zo? z zo |
5. Additional parameters for regular expressions:
g: | Representatives can perform global matching |
---|---|
i: | Represents case-insensitive matching |
M: | Representatives can perform multi-line matching |
//手机号号码
Var phone=/^(13|15|18)\d{
9}$/
//密码是6-16位
Var yhmzz=/^[A-Za-z0-9](\w|-|.){
2,16}[A-Za-z0-9]$/
//昵称由汉字字母数字下划线以及@ ! # ¥ % & * 组成长度是 4—20
Var nickname=$("#nickname").val();
Var nickzz=/^[\uEOO-\u9fa5]|\W|[@!$%&*]$/;
//正则表达式的验证
if(nickzz.test(nickname)){
//长度验证
//将一个中文编程 变成两个字符
lam 中文 lamxxxx
Var aa=nickname.replace(/[\u4Eoo-\u9fa5]/,'xx');
Var len==aa.length;
if(len>=4&&len<=20){
//符合条件
}else{
//不符合条件
}
}
//长度验证
//中文一个字占了两个位置
//获取的值 nickname
Replace(/cat/,dog)
6. Methods of string object
method | describe |
---|---|
Match() | find one or more regular expression matches |
Search() | Search matches the regular expression |
Replace() | Replace strings that match regular expressions |
Split() | Split a string into an array of bitstrings |
Match() | The method can retrieve the specified value in the string and find a match of multiple regular expressions. This method is similar to the indexof() method, but indexof() puts back the position of the string instead of the specified value |
Replace() | method is used to replace some characters in a string with some other characters. |
7. Commonly used characters in regular expressions
symbol | describe |
---|---|
// | Represents the start and end of a pattern |
^ | match the beginning of the string |
$ | matches the end of the string |
\s | any blank string |
\S | any non-blank string |
\d | – |
\w | Match a number. Underscore or letter, equivalent to [az AZ 0-9] |
\W | Any non-word character, equivalent to [^az AZ 0-9] |
。 | .Any character except newline |
8. Repeated characters for regular expressions
symbol | describe |
---|---|
{n} | Match the previous item n times |
{n,} | Match the previous item 1 or more times |
{n,m} | Match the preceding .../item at least n times, but no more than m times |
* | Match the previous... item 0 or more times, equivalent to {0,} |
+ | Match the previous item one or more times, equivalent to {1,} |
? | Match the previous item 0 or 1 time, that is, the previous item is optional, equivalent to {0,1} |