Custom Phone Number Validation in Jquery Validator
This post all about the custom phone number validation in jquery. I have seen many developers are facing problem when they are using jquery validation plugin for form validation, the plugin by default rules are there. But in Some cases if we want to validate our custom field like Indian Phone/ mobile number for example +91 – 7993491890 then it is not available in this jquery validation plugin, In that case, we need to customize this. Here we go below example,
It is very easy to implement our custom rules.
Below Custom phone/ mobile number validation can take special char like +, – and space.
+91 – 7993491890 or +91 7993491890
Step-1:
1 2 3 4 5 6 7 8 9 |
<script type="text/javascript"> $.validator.addMethod('customphone', function (value, element){ return this.optional(element) || /^[0-9 +-]+$/.test(value); }, "Please enter a valid phone number"); </script> |
Step-2:
1 2 3 4 5 6 7 8 9 10 |
<script type="text/javascript"> $(document).ready(function () { $('#events-form').validate({ rules: { customer_mobile: 'customphone', }, }); }); </script> |
If any Other type of validation required please feel free to put your comment below, we will try to solve your problem.