邮政编码验证仅支持4位数字,不起作用。

huangapple go评论57阅读模式
英文:

Postcode verify only 4 digits not working

问题

这是我的用于验证邮政编码的Java代码,它可以检查空字段,但我无法让它验证是否只包含4位数字。

// 检查邮政编码
const postcode = document.getElementById('postcode');
const postcodeValue = postcode.value.trim();
const postcoder = /^[0-9]\d{4}$/;
if (postcodeValue === '') {
  setErrorFor(postcode, '邮政编码不能为空');
} else if (!postcoder.test(postcodeValue)) {
  setErrorFor(postcode, '邮政编码必须包含4位数字');
} else {
  setSuccessFor(postcode);
}

我希望它在出现多于或少于4位数字的情况下或包含数字以外的任何内容时显示错误消息。

英文:

this is my java for verifing a postcode it is working for the blank field but i cant get it to verify that it only contains the 4 digits

//  Check postcode
  const postcode = document.getElementById('postcode');
  const postcodeValue = postcode.value.trim();
  const postcoder = /^[0-9]\d{4}$/;
  if(postcodeValue === () {
    setErrorFor(postcode, 'Postcode cannot be blank');
  } else if(!postcoder.test(postcodeValue)) {
    setErrorFor(postcode, 'Postcode must contain 4 digits');
  } else {
    setSuccessFor(postcode);
  }

I want it to show an error message if there is more or less than 4 digits or if there is anything other than numbers.

答案1

得分: 2

这个正则表达式,^[0-9]\d{4}$ 匹配

  • ^:文本开头,接着是
  • [0-9]:一个US-ASCII的十进制数字(0-9),接着是
  • \d{4}:正好4个US-ASCII的十进制数字(0-9),最后是
  • $:文本结尾

所以它匹配5个数字。

英文:

This regular expression, ^[0-9]\d{4}$ matches

  • ^: start-of-text, followed by
  • [0-9]: a US-ASCII decimal digit (0-9), followed by
  • \d{4}: exactly 4 US-ASCII decimal digits (0-9), followed by
  • $: end-of-text

So it matches 5 digits.

huangapple
  • 本文由 发表于 2023年6月1日 07:46:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定