我使用JavaScript的正则表达式字符串:JavaScript的正则表达式的数字不匹配,包含单零
val.match(/\d*$/)
它的工作如预期当字符串为“0”的所有号码的期望。单个零值不匹配。我如何包含它?
测试用例:
<html>
<head>
<script src="scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
</head>
<body>
<input type="text" id="txtRange" />
<div id="result" />
<script type="text/javascript">
var rangeValidate = function (val, min, max) {
if (val == "") {
return "You must specify a number between " + min + " and " + max;
} else if (val.match(/\d*$/) == false) {
return "You must specify a number between " + min + " and " + max;
} else if ((val < min) || (val > max)) {
return "You must specify a number between " + min + " and " + max;
} else {
return "";
}
return "You must specify a number between " + min + " and " + max;
}
$('#txtRange').bind('change', function(){
var value = $('#txtRange').val();
var resultText = rangeValidate(value, 0, 8);
$('#result').html(resultText);
});
</script>
</body>
</html>
@Felix King - 在JavaScript中,字符串“0”肯定是**不是**“falsy”。这是一个非空字符串,因此它是“真实的”。 – Pointy 2011-03-23 19:01:28
@Pointy:至少在Chrome控制台中,输入“”0“== false”会给我“真”。我应该如何理解?我看到......还有一些转换正在进行中。 – 2011-03-23 19:02:37
尝试'!!“0”'而不是'if(“0”)alert('falsy');' – Pointy 2011-03-23 19:03:54