0

At the contact form I have a field name as Extension (new_ext). and at the onChnage event I want to do check weather the user has enter the number or anything else. I have the following piece of code.

function formatPhone(phonenum)
{
var ext =phonenum.getEventSource().getValue();
  var reg = /^[0-9]$/;
if(ext.match(reg))
{
alert("Valid");
}
else
{
alert("invalid");
}
}

It returns me always invalid even if I enter a letter or a number or both.

I want to seek your kind suggestions and help regarding this.

Shahai Ali
  • 15
  • 5
  • your regex matches 1 digit only. If there is more than 1 digit it will return false. I think you want `/^[0-9]+$/`. – Bryan Elliott Feb 06 '14 at 19:56
  • You should strip all non-digits like so: `phonenum.replace(/[^0-9]+/g, '')` then further validate the numbers/length as a legitimate phone number. You also may find [this](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) useful. – tenub Feb 06 '14 at 20:18

1 Answers1

0

Try this one "^\d+$", just check for null values before if you need to.

Already asked here Regex allow a string to only contain numbers 0 - 9

Community
  • 1
  • 1
djluis
  • 362
  • 5
  • 19