【问题标题】:Validating value for Correct format验证正确格式的值
【发布时间】:2014-04-10 06:04:36
【问题描述】:

我有多个文本框,其格式应为 XXX.XX,其中 X 可以是用户输入的任何数字。我也有一个提交按钮。因此,在提交之前,如果文本框具有值,则必须遵循给定的格式。 我正在像下面这样进行。下面是我想要的代码+步骤:::

我必须遍历 div 元素并检测每个文本框,然后对于每个文本框,如果它有值,我必须对其进行验证。

 <div id="DivFeeContent" style="float: left; width: 100%;">
         <input type="text" style="width: 95px;" maxlength="3"  class="validate" />
         <input type="text" style="width: 95px;" maxlength="3"  class="validate" />
         <input type="text" style="width: 95px;" maxlength="3"  class="validate" />
         <input type="text" style="width: 95px;" maxlength="3"  class="validate" />
</div>

代码

function CheckpricePrice() {
    $("#DivFeeContent input:text").each(function () {
         var Value= $(this).val();
         if(Value!='')
          {
                // Check the Format . It should be XXX.XX
                  if (Value == matches)
                  {
                   }
                   else{ return false;}   
          }

         });

     Here Goes Submission of the form
}

【问题讨论】:

    标签: javascript jquery logic


    【解决方案1】:

    使用正则表达式:

    if(/^\d+\.\d{2}$/.test(Value))
    {
          //valid number
    }
    

    这基本上意味着匹配点后有 2 位数字的任何数字,因此 XXXXX.XX 或 XXX.XX 将起作用。如果您只想要 XXX.XX,那么您可以将模式更新为 /^\d{3}\.\d{2}$/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多