how to match a input value with the regular expres

2019-09-07 02:20发布

问题:

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.

回答1:

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