search

Home  >  Q&A  >  body text

javascript - js test regular expression encountered a strange problem

    let reg=/^$|^[1-9]\d*$/;
    if(!reg.test(course1)){
      showToast.msg('套餐1只能输入非零正整数');
      return false;
    }
    if(!reg.test(course2)){
      showToast.msg('套餐2只能输入非零正整数');
      return false;
    }
    

The regular expression written as above will work, but the regular expression written as follows will be invalid. Entering letters can also pass the verification!

if((!reg.test(course1))&&(!reg.test(course2))){
  showToast.msg('套餐只能输入非零正整数');
  return false;
}
伊谢尔伦伊谢尔伦2734 days ago779

reply all(1)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-07-05 10:53:10

    course1='1', course2='a', this case will not enter your method.
    It should be the relationship of ||.
    The code is modified as follows:

    if((!reg.test(course1))||(!reg.test(course2))){
      showToast.msg('套餐只能输入非零正整数');
      return false;
    }

    reply
    0
  • Cancelreply