我正在尝试验证 5-60 之间的数字
<div class="col-md-3">
<input type="text" name="gd_time" id="gd_time" placeholder="Should b/w 5 to 60 " class="form-control">
</div>
我已经尝试过这个脚本但不起作用:
//gd time
var gd_time = $("#gd_time").val();
var gdtimereg = [1-9]\d*|0\d+;
if((gd_time == '') || (!gdtimereg.test(gd_time)))
{
$("#gd_time").css({"border-style": "solid", "border-color": "red" });
$("#showMessage").html('Please enter the gd time should be 5 to 60 minutes');
$("#gd_time").focus();
return false;
}
else{
$("#gd_time").css({"border-style": "solid","border-color": "#E9E9E9"});
}
最佳答案
将值转换为 Number
并使用 if
语句验证它。
检查下面的示例。
//gd time
var gd_time = new Number($("#gd_time").val());
if (gd_time >= 5 && gd_time <= 60) {
$("#gd_time").css({
"border-style": "solid",
"border-color": "#E9E9E9"
});
} else {
$("#gd_time").css({
"border-style": "solid",
"border-color": "red"
});
$("#showMessage").html('Please enter the gd time should be 5 to 60 minutes');
$("#gd_time").focus();
}
input {
outline: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="gd_time" />
关于javascript - jquery 仅验证 5-60 之间的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39911778/