Jump to content

JS & RegEx Problems


unkwntech

Recommended Posts

I have this code to check a form field that should contain a phone number, however when I run the function that this is in I get the following on the console:

Chrome:

Uncaught SyntaxError: Invalid regular expression: //(?[0-9]{3})?[ -]?[0-9]{3}[ -]?[0-9]{4}/i/: unrecognized character after (?

FireFox:

Error: invalid quantifier ?[0-9]{3})?[ -]?[0-9]{3}[ -]?[0-9]{4}/i

if(!ph.value.match('/\(?[0-9]{3}\)?[ -]?[0-9]{3}[ -]?[0-9]{4}/i'))
  {
 	console.log('Phone Number is not valid');
	ph.className = 'errorOnField';
}

 

RegEx buddy says that this RegEx is valid for JS but obviously there is a problem, what am I doing wrong?

 

 

Link to comment
https://forums.phpfreaks.com/topic/145483-js-regex-problems/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.