javascript - How to match empty or regular rules?
欧阳克
欧阳克 2017-06-26 10:51:20
0
4
895

For example: How to judge whether the email conforms to the rules or is not filled in

欧阳克
欧阳克

温故而知新,可以为师矣。 博客:www.ouyangke.com

reply all(4)
大家讲道理

Simply use the delimiter ^$ to match the empty string, or use | to achieve the matching effect you want.

eg: Match mobile phone number, or empty

/^$|1[34578]\d{9}/
滿天的星座
if(email && email.trim() && /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/.test(email.trim())===false){
   // 填了邮箱并且邮箱不符合规则
}
学习ing
//判断输入内容是否为空 
function IsNull(){ 
var str = document.getElementById('str').value.trim(); 
if(str.length==0){ 
alert('对不起,文本框不能为空或者为空格!');//请将“文本框”改成你需要验证的属性名称! 
} 
} 
function IsEmail() 
{ 
var str = document.getElementById('str').value.trim(); 
if(str.length!=0){ 
reg=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; 
if(!reg.test(str)){ 
alert("对不起,您输入的字符串类型格式不正确!");//请将“字符串类型”要换成你要验证的那个属性名称! 
} 
} 
}
漂亮男人
/^(\w+(\.\w+)*@\w+(\.\w+)+|)$/

Among them, use /^...$/ to define the beginning and end, and there is a group in the middle. Pay attention to the vertical line at the end of the group. If nothing is written after the vertical line, it means it is empty.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template