简体   繁体   English

最低和最高价格验证

[英]Validation for Minimum and Maximum Price

I have 2 textbox one is of Minimum Price and other is of Maximum Price.I want the validation when one enter the Minimumm price greater than Maximum Price that "you have enter the Minimum Price greater than Maximum Price". 我有2个文本框,一个是“最低价格”,另一个是“最高价格”。我要验证当一个输入“最低价格”大于“最大价格”时是否“您输入的最低价格大于最大价格”。 I have tried Custom Validation for this and even written ajax for this as well My code goes as follows: 我为此尝试了“自定义验证”,甚至为此也编写了ajax。我的代码如下:

<input type="text" id="MinimumPrice"/>
<input type="text" id="MaximumPrice"/>

$('#MaximumPrice').blur(function () {
            var url1 = "/Profile/CheckMaxPrice";
            var max = $('#MaximumPrice').val();
            var min= $("#MinimumPrice").val();
            $.get(url1, { maximum: max, minimum: min }, function (data) {
                if (data.maximum<data.minimum) {
                    alert("ok");
                }
            });
        });

and the function as 和功能为

[AllowAnonymous]
        [HttpPost]
        public ActionResult CheckMaxPrice(int maximum, int minimum)
        {
            if (maximum < minimum)
            {

                return Json("maximum", JsonRequestBehavior.AllowGet);

            }
            else 
            {
                return Json("minimum", JsonRequestBehavior.AllowGet);
            }
        }

and even written the script as well as follows: 甚至编写脚本如下:

$("#MaximumPrice").blur(function () {
           var min = $("#MinimumPrice").val();
            var max = $("#MaximumPrice").val();
            if (min > max)
            {
                //$("#MaximumPrice").append("<p>Min Price can not be greater tham Max Price</p>");
                alert("Min Price can not be greater than Max Price");
            }
        });

but not getting the proper result. 但没有得到正确的结果。

To compare numbers, you're going to need to turn the values into numbers, otherwise 17 will be less than 2. 要比较数字,您需要将值转换为数字,否则17会小于2。

var min = parseInt($("#MinimumPrice").val(),10);
var max = parseInt($("#MaximumPrice").val(),10);
if (min > max){
    alert("Min Price can not be greater than Max Price");
}

I'm assuming you're using whole numbers, otherwise you can use parseFloat() 我假设您使用的是整数,否则可以使用parseFloat()

$("#MinimumPrice, #MaximumPrice").change(function (e) {
    var lil = parseInt($("#MinimumPrice").val(), 10);
    var big = parseInt($("#MaximumPrice").val(), 10);
    $('#lil').text(lil);
    $('#big').text(big);
    if (lil > big) {
        var targ = $(e.target);
        if (targ.is("#MaximumPrice")) {
            alert("Max must be greater than Min");
            $('#MaximumPrice').val(lil);
        }
        if (targ.is("#MinimumPrice")) {
            alert("Min must be less than Max");
            $('#MinimumPrice').val(big);
        }
    }
});

jsFiddle 的jsfiddle

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

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