另一个jquery验证问题

时间:2010-01-28 16:29:23

标签: jquery validation

我正在尝试验证名为promoRent的字段,如果只输入了一个数字。它不是必填字段,但如果输入则必须大于lotRent。我有:

 jQuery.validator.addMethod("PRgreaterThanLotRent", function(value, element) {
  return parseFloat($("#lotRent").val().replace(/[^\d\.]/g, '').replace(['.00'],''))     <   parseFloat($("#promoRent").val().replace(/[^\d\.]/g, '').replace(['.00'],''));
 });


   $("#pricingForm").validate({
  rules: { salesPrice: "required",
     Rent: { required: true, greaterThanLotRent : true }, 
  promoRent : { required: function() { if ($("#promoRent").val() != '') {  PRgreaterThanLotRent : true }; } }
 },
  messages: { salesPrice: "<br/><span style='color:red'>Required!</span>",
  mhRent: { required: "<br/><span style='color:red'>Required!</span>",
      MHgreaterThanLotRent : "<br/><span style='color:red; '>Must be greater<br> than Lot Rent</span>" } , 
  promoRent : { required: "",
       PRgreaterThanLotRent : "<br/><span style='color:red; '>Must be greater<br> than Lot Rent</span>" } 
 }
 }).form(); 

我正在做一些愚蠢的事情,因为即使我输入的数量少于许多,验证也不会运行。有任何想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

这很有效。

  ,promoRent : { PRgreaterThanLotRent : true }

然后修改PRgreaterThanLotRent函数,如下所示:

 jQuery.validator.addMethod("PRgreaterThanLotRent", function(value, element) {
        if ($("#promoRent").val() != '') {                                                  
        return parseFloat($("#lotRent").val().replace(/[^\d\.]/g, '').replace(['.00'],'')) <  parseFloat($("#promoRent").val().replace(/[^\d\.]/g, '').replace(['.00'],''));
        } else {
        return true;
        }
    });

sweeeeeeet ......

相关问题