Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Rule validation for allowing integer and space

My current validator for accepting integer is

$( "#UserForm" ).validate({
  rules: {
    contact: {
      required: true,
      number: true,
      minlength: 6,
    }
  }
});

It works fine

But now i want to make change in it so as to accept spaces also.

What should be my code?Please help i'm beginner in jquery

like image 750
nilesh Avatar asked Apr 15 '26 05:04

nilesh


1 Answers

You can use the pattern rule defined in the additional-method.js file

jQuery(function($) {
  $("#UserForm").validate({
    rules: {
      contact: {
        required: true,
        pattern: /^[\d\s]+$/,
        minlength: 6,
      }
    },
    messages: {
      contact: {
        pattern: 'Please enter digits or space characters only'
      }
    }
  });
});
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/jquery.validate.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.12.0/additional-methods.js"></script>
<form id="UserForm" method="post" action="">
  <div>
    <input name="contact" />
  </div>
  <input type="submit" class="button" value="Submit" />
</form>
like image 118
Arun P Johny Avatar answered Apr 16 '26 21:04

Arun P Johny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!