比较jquery中Integer值的两个文本框和数字验证

时间:2017-05-11 05:24:48

标签: php jquery

我有两个文本框。一个是从日期开始,另一个是迄今为止。我用文本框验证数字。但我的要求是,如果我填写日期700而不是日期接受大于700 这是我的HTML



$("#from").keypress(function(e) {
  //if the letter is not digit then display error and don't type anything
  if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
    //display error message
    $("#errmsg").html("Digits Only").show().fadeOut("slow").css("color", "red");;
    return false;
  }
});
$("#to").keypress(function(e) {
  //if the letter is not digit then display error and don't type anything
  if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
    //display error message
    $("#errmsg").html("Digits Only").show().fadeOut("slow").css("color", "red");;
    return false;
  }
});
$("#searchsubmit").click(function() {
  var to = $("#to").val();
  var from = $("#from").val();
  if (to == "" && from == "") {
    //alert('Please Provide Era Value For From And To');
    $("#response").html("Please Provide Era Value For From And To").show().fadeOut(3000).css("color", "red");
    //window.location();
  } else {
    var info = 'to=' + to + '&from=' + from; {
      if (info) {
        //alert(info);
        window.location.href = "timeline.php?from=" + from + '&to=' + to;;
      } else {
        alert('No Data Found');
      }
    }
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
<span class="info"> From </span>
<input class="erainput" name="from" id="from" type="text" placeholder="UP To 700" required />
</span>
<span class="eraform">
    <span id="errmsg"></span>
<span class="info"> To </span>
<input class="erainput" name="to" id="to" type="text" placeholder="2000" required />
</span>
<span class="eraform">
  <button type="submit" id="searchsubmit" name="submit" class="eraBtn"> Search <i class="fa fa-search"></i> </button>
</span>
&#13;
&#13;
&#13;

由于

2 个答案:

答案 0 :(得分:1)

  1. 干(不要重复)
  2. 使用parseInt
  3. 使用Ajax
  4. test isNaN
  5. 测试粘贴(输入时)
  6. 使用按钮 - 请查看对HTML的更改,我还添加了响应div
  7. 永远不要以name="submit"形式调用任何内容隐藏表单提交事件
  8. info将始终为真 - 您没有空字符串
    $("#from, #to").on("keypress input", function(e) {
      //if the letter is not digit then display error and don't type anything
      if (isNaN($(this).val()) || (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57))) {
        //display error message
        $(this).val("");
        $("#errmsg").html("Digits Only").show().fadeOut("slow").css("color", "red");;
        return false;
      }
    });
    $("#searchsubmit").click(function() {
      var to = parseInt($("#to").val(), 10);
      var from = parseInt($("#from").val(), 10);
      if (to == ""    || from == "" ||
          isNaN(to)   || isNaN(from)|| // not likely possible now
          to   < from ||
          from < 700  ||
          to   > 2000 
      ) {
        $("#response").html("Please provide valid Era values for From and To").show().fadeOut(3000).css("color", "red");
      } else {
        var info = 'to=' + to + '&from=' + from;
        console.log(info);
        $("#response").load("timeline.php?from=" + from + '&to=' + to);
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
    <span class="info"> From </span>
    <input class="erainput" name="from" id="from" type="text" placeholder="UP To 700" required />
    </span>
    <span class="eraform">
        <span id="errmsg"></span>
    <span class="info"> To </span>
    <input class="erainput" name="to" id="to" type="text" placeholder="2000" required />
    </span>
    <span class="eraform">
      <button type="button" id="searchsubmit" class="eraBtn"> Search <i class="fa fa-search"></i> </button>
    </span>
    <div id="response"></div>

答案 1 :(得分:0)

使用以下代码并根据您的要求进行修改。

&#13;
&#13;
$("#searchsubmit").click(function (e) {
  var from = parseInt($("#from").val());
  var to = parseInt($("#to").val());
  if(to < from)
  {
     alert("TO value must be greater than FROM");
  }
  else 
  {
     //submit form
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="info"> From </span>
<input class="erainput" name="from" id="from" type="text" placeholder="UP To 700" required  />
</span>
<span class="eraform">
<span id="errmsg"></span>
<span class="info"> To </span>
<input class="erainput" name="to" id="to"  type="text" placeholder="2000" required  />
</span>
<span class="eraform">
<button type="submit" id="searchsubmit" name="submit" class="eraBtn"> Search <i class="fa fa-search"></i> </button>
</span>
&#13;
&#13;
&#13;