Javascript / jQuery-检查输入value1> value2

时间:2018-08-13 02:43:54

标签: javascript php jquery codeigniter

我创建一个表单并具有2个字段,因此当Value1> Value2时,我希望显示消息错误并且不处理该表单。

表单代码:

<?php echo form_open_multipart('Service/add_service_arsitek','name="formservice" onsubmit="return validateForm()"');?>
  <div class="flex-w flex-m m-r-20 m-tb-5">
        <input class="stext-104 cl2 plh3 size-117 bor13 p-lr-20 m-r-10 m-tb-5" name="minimum" id="minimum" placeholder="Budget Minimum Anda" required>
        <div class="flex-c-m stext-101 cl2 size-118 ">
          <input class="stext-104 cl2 plh4 size-117 bor13 p-lr-20 m-r-10 m-tb-5" name="maximum" id="maximum" placeholder="Budget Maximum Anda" required>
        </div>
      </div>
<input type="submit" value="Dapatkan Penawaran" class="flex-c-m stext-101 cl0 size-121 bg3 bor1 hov-btn3 p-lr-15 trans-04 pointer">
</form>

JavaScript代码:

<script type="text/javascript">
function validateForm(){
var x = document.forms["formservice"]["minimum"].value;
var y = document.forms["formservice"]["maximum"].value;
if (x > y || x == y) {
 swal("Error!", "opps sorry! your input is wrong", "error")
    return false;
}
}
</script>

1 个答案:

答案 0 :(得分:0)

使用parseIntparseFloat。当您从文本类型输入文本中获取价值时,其text

 <script type="text/javascript">
 function validateForm(){
    var x = parseFloat($("#minimum").val());
   var y = parseFloat($("#maximum").val());
     if (x >= y) {
          swal("Error!", "opps sorry! your input is wrong", "error")
         return false;
    }
  }
  </script>