开发者

jQuery validation plugin - numbers only, min & max length - space issue

开发者 https://www.devze.com 2023-04-03 11:19 出处:网络
I\'m using jQuery Validation (PLUGIN URL) plugin for most of my form validation: I have following example (LIVE EXAMPLE):

I'm using jQuery Validation (PLUGIN URL) plugin for most of my form validation:

I have following example (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 />&l开发者_如何学Got;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
        }

    }

});

I have two issues, both relating to space usage.

  • min & max length will count space as an character
  • if number = true and space is used, it will return error as space is not a number

Is there any workaround this? Have any of you bumped in on the same problem? Please note I want to keep allowing to type in space (for readability).


You can add a beforeValidation callback to send the value without spaces, take a look here: Jquery Validator before Validation callback, it seems to be what you're looking for


a very smiple solution is :

replace the type of the phone input as the following :

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

and you would never have issues with spaces and stuff, just used that in my code !

enjoy

0

精彩评论

暂无评论...
验证码 换一张
取 消