Mouss Mouss - 1 year ago 85
AngularJS Question

Regex not working in angular JS

I am trying to build a custom regex for phone number validation, basically I allow any string longer than 10 character and that can contain only those characters : any number, "+", ".", "(", ")", "-" and white space.

Here is my regex :

var myRegex = new RegExp('^[0-9|\+|\.|\(|\)|\-|\s]{10,}$');

I don't know why this regex is not matching "01234567 89" or "01234567-89", I have tested it on a regex validator online and it is supposed to work.

I have plunkered my issue :

Answer Source

Change your regex to this to match your regex validator online example:

var myRegex = new RegExp(/^[0-9|\+|\.|\(|\)|\-|\s]{10,}$/);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download