简体   繁体   English

验证输入字段的值大于另一个

[英]Verifying that the value of a input field is greater than another

The maximum value must be greater than the minimum value. 最大值必须大于最小值。 It's working fine but when I put in '12345' as the maximum value then even if the minimum value is less, it shows the error shown in the image. 它工作正常,但是当我将“ 12345”作为最大值输入时,即使最小值较小,它也会显示图像中显示的错误。


 if (max < min) {
        chkFeeValues = false;

        if (chkFeeValues) {
            $('.errorMaxFee').html('');
        } else {
            $('.errorMaxFee').html(html);
        }

        $(document).ready(function() {
                    var isValOk = false;
                    var isPerOk = false;
                    //Value
        $(document).on("change", "#MinValue", function() {
            if (parseInt($("#MinValue").val()) > parseInt($("#MaxValue").val())) {
               $("#MaxValueError").text("Max Value should be greter than or equals to Min Value");
               isValOk = false;
            } 
else {
            $("#MaxValueError").text("");
            isValOk = true;
          }
       });

在此处输入图片说明

You're only listening to changes made on the #MinValue input field so you'll never get it to trigger when changing the #MaxValue input field. 您只在听在#MinValue输入字段上#MinValue更改,因此在更改#MaxValue输入字段时永远不会触发它。

Change your handler to $(document).on('change', '#MinValue, #MaxValue', function() {}); 将您的处理程序更改为$(document).on('change', '#MinValue, #MaxValue', function() {}); in order to revalidate as soon as you change either input fields. 为了在您更改两个输入字段后立即进行重新验证。

The problem is that you're checking for validity only when you change the min value. 问题是您仅在更改最小值时才检查有效性。 You have to check when you change the max also. 您还必须检查何时更改最大值。 So refactor the code this way: 因此,以这种方式重构代码:

    function checkValues() {
        if (parseInt($("#MinValue").val()) > parseInt($("#MaxValue").val())) {
            $("#MaxValueError").text("Max Value should be greter than or equals to Min Value");
            isValOk = false;
        }
        else {
            $("#MaxValueError").text("");
            isValOk = true;
        }
    }

$('#MinValue').on('change', checkValues);
$('#MaxValue').on('change', checkValues);

I think that you need something like that: 我认为您需要这样的东西:

 function FormCtrl($) { var self = this; var MIN_VALUE = 1000; self.minValueAccepted = MIN_VALUE; self.min = $('#ValA'); self.field = $('#ValB'); self.formFeedback = function(message, success) { var type = success ? 'info' : 'danger'; console.log(message, success) return $('#formFeedback').html('<p class="text-'+ type +'">' + message + '</p>'); }; self.updateField = function(event) { var value = Number(self.field.val()); var isValid = false; var message = 'Try Again'; if(value > self.minValueAccepted) { isValid = true; message = 'You Are Hired'; } return self.formFeedback(message, isValid); }; self.updateMinValue = function(event) { self.minValueAccepted = Number(self.min.val()); }; this.min.change(self.updateMinValue); this.field.change(self.updateField); this.min.val(MIN_VALUE); } jQuery(document).ready(FormCtrl); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" > <section class="container"> <div class="row"> <form id="sampleForm"> <div class="col-xs-6"> <label for="valA">Value A</label> <input class="form-control" type="number" id="ValA"/> </div> <div class="col-xs-6"> <label for="valB">Value B</label> <input class="form-control" type="number" id="ValB"/> </div> <div class="row"> <div class="col-xs-12" > <div id="formFeedback" style="padding: 1em; text-align:center;"></div> </div> </div> </form> </div> </section> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM