2

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

1 Answer 1

10

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.