javascript - jquery 仅验证 5-60 之间的数字

标签 javascript php jquery arrays validation

我正在尝试验证 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/

相关文章:

javascript - 动态生成表格时使用按钮时AJAX函数触发一次但不会触发第二次

javascript - 如何使用 ApiSauce 传递不记名授权 token

javascript - ReactJS:在 JQuery 事件监听器函数中调用 'this'

javascript - 在 $.ajax Success 方法中拒绝 jQuery Promise

javascript - 为什么我的 Javascript 函数有时只能工作?

javascript - 二维中点分割逻辑

JavaScript 编码标准

php - ssh2_scp_send() 和 ssh2_scp_recv() 函数在 php 中不起作用。为什么?

php - 从 wordpress 访问和显示 wordpress 帖子

php - 将 css 文件添加到 Kohana 模板