【问题标题】:jquery validate only numbers between 5-60jquery 仅验证 5-60 之间的数字
【发布时间】:2016-10-07 07:29:41
【问题描述】:

我正在尝试验证 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 (num &gt;= 5 &amp;&amp; num &lt;= 60) { valid } else { invalid }
  • 正则表达式应该被/包围

标签: javascript php jquery arrays validation


【解决方案1】:

将值转换为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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    相关资源
    最近更新 更多