2011-09-06 84 views
5

我使用jQuery验证(PLUGIN URL)插件,我的大多数表单验证:jQuery的验证插件 - 只有数字,最小和最大长度 - 空间问题

我有下面的例子(LIVE EXAMPLE):

FORM:

<form class="form" id="feedback-form" method="post" action="#"> 


    <label for="phone">Contact number:</label> 
    <input type="text" value="" id="phone" name="phone" class="required"> 
    <br class="clr"> 

    <div class="form-buttons"> 
     <input id="submit" type="submit" value="Submit" class="button" > 
    </div> 
    <br class="clr"> 
    <br /><br /> 

</form> 

jQuery的:

$("#feedback-form").validate({ 
    rules: { 

     phone: { 
      number: true, // as space is not a number it will return an error 
      minlength: 6, // will count space 
      maxlength: 9 
     } 

    } 

}); 

我有两个问题,都与空间的使用。

  • 分钟&最大长度将计算空间作为一个字符
  • 如果number = true和空间的使用,它将作为空间不是数字

返回错误有任何解决方法吗?有没有人遇到同样的问题?请注意我想继续允许输入空格(为了便于阅读)。

回答

4

您可以添加beforeValidation回调不带空格送价值,看一看这里:Jquery Validator before Validation callback,这似乎是你在找什么

+1

这将阻止用户进入空间。不是完美的解决方案。由于我想保持空间分离的可读性,但不包括字符数量的空间。 – Iladarsda

0

非常smiple的解决方案是:

更换类型的手机输入如下:

<input id="phone" type="number" maxlength="10" size="2" max="99" style="width:36px;" required /> 

和你永远不会有空间和东西的问题,只是在我的代码中使用!

享受