英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论