For example: How to judge whether the email conforms to the rules or is not filled in
温故而知新,可以为师矣。 博客:www.ouyangke.com
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){ // 填了邮箱并且邮箱不符合规则 }
//判断输入内容是否为空 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.
/^...$/
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
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.