javascript - js test regular expression encountered a strange problem
伊谢尔伦
伊谢尔伦 2017-07-05 10:51:10
0
1
739
    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;
}
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
淡淡烟草味

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;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template