search

Home  >  Q&A  >  body text

javascript - How to match empty or regular rules?

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

欧阳克欧阳克2771 days ago923

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-06-26 10:53:20

    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}/

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-26 10:53:20

    if(email && email.trim() && /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/.test(email.trim())===false){
       // 填了邮箱并且邮箱不符合规则
    }

    reply
    0
  • 学习ing

    学习ing2017-06-26 10:53:20

    //判断输入内容是否为空 
    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("对不起,您输入的字符串类型格式不正确!");//请将“字符串类型”要换成你要验证的那个属性名称! 
    } 
    } 
    }

    reply
    0
  • 漂亮男人

    漂亮男人2017-06-26 10:53:20

    /^(\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.

    reply
    0
  • Cancelreply