Syed Salman Raza Zaidi
Syed Salman Raza Zaidi

Reputation: 2192

JS Numeric and Hyphen validation

I am using Javascript to validate phone number with hyphen. It is validating only one hyphen but not allowing two hyphens.

I want to validate this format:

123-456-7891

How can I do this with JS? This is my function

 function numericValidation(phoneno) {
     var numbers = /^\d+((;\d+)*|-\d+)?$/;
     if (phoneno.match(numbers)) {
         alert('Your input is valid');
         return true;
     } else {
         alert('Please enter in (123-456-7891) format');
         return false;
     }
 }

Upvotes: 1

Views: 1727

Answers (1)

user3917789
user3917789

Reputation: 90

 function numericValidation(phoneno) {
   var numbers =/^\d+(-\d+)*$/;
   if (phoneno.match(numbers)) {
     alert('Your input is valid');
     return true;
   }
   else {
     alert('Please enter in (123-456-7891) format');
     return false;
   }
 }

Upvotes: 2

Related Questions