gpt4 book ai didi

javascript - 我想要javascript中年龄字段的正则表达式

转载 作者:行者123 更新时间:2023-11-30 13:14:36 25 4
gpt4 key购买 nike

 var text6 = document.getElementById("text6").value;
var regularExpression1 = /^[1-9]?[0-9]{1}$|^100$/;

if (!text6.match(regularExpression1)){
alert("Please enter Valid Age");
return false;
}

这段代码不能正常工作

最佳答案

不要使用正则表达式。使用此函数检查某物是否为数字

function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}

对于你的情况

function isIntegerBetween0And100(n) {
return isNumber(n) && n>0 && n<=100 && n%1===0;
}

快得多:)

关于javascript - 我想要javascript中年龄字段的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12527388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com