Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery phone number validation allow spaces, minimum of 8 digits

How can I allow spaces within an a numbers only validation, with a minimum of 8 digits? Phone numbers are much easier to type in when spaces are allowed. e.g. 0400 123 456, 9699 1234.

Here's my code so far, I've only got the minimum 8 digits validation working:

jQuery.validator.addMethod(
  "phone",
  function(phone_number, element) {
    return this.optional(element) || /^\d{8}$/.test(phone_number);
  },
    "Please enter numbers only"
);
like image 214
JV10 Avatar asked Jun 14 '12 01:06

JV10


Video Answer


1 Answers

Remove the space before validating:

return this.optional(element) || /^\d{8,}$/.test(phone_number.replace(/\s/g, ''));

This way you retain the space

like image 79
Andreas Wong Avatar answered Sep 20 '22 20:09

Andreas Wong