jQuery验证插件 - 仅限数字,最小和最大长度 - 空间问题

Ila*_*sda 5 jquery jquery-validate

我正在使用jQuery Validation(PLUGIN URL)插件进行大部分表单验证:

我有以下示例(实际示例):

形成:

 <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>
Run Code Online (Sandbox Code Playgroud)

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
        }

    }

});
Run Code Online (Sandbox Code Playgroud)

我有两个问题,都与空间使用有关.

  • 最小和最大长度将空间计为一个字符
  • if number = true和space一起使用时,它会返回错误,因为空格不是数字

这有什么解决方法吗?有没有人遇到过同样的问题?请注意我想继续允许输入空格(为了便于阅读).

Sou*_*sou 4

您可以添加 beforeValidation 回调来发送不带空格的值,请看这里:Jquery Validator before Validation callback,它似乎就是您正在寻找的