4

Can I use preg_match to validate phone number in jQuery? Here is my code which does not work:

if (!preg_match("/^[0-9]{3}-|\s[0-9]{3}-|\s[0-9]{4}$/", phone.val() ))  {
            phone.addClass("needsfilled");
            phone.val(phonerror);
        }

HTML <input id="phone" type="text" value="" name="phone" />

peterh
  • 11,875
  • 18
  • 85
  • 108
Mary
  • 355
  • 4
  • 9
  • 17
  • 4
    `preg_match` is a PHP function, not a JavaScript function, hence you cannot use it. You should have a look here: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions – Felix Kling May 17 '11 at 13:28

4 Answers4

12

Javascript includes a regular expression engine, which is accessed via the string.match(), string.replace() and string.split() functions.

For example:

var mystring = "this is a sentence";
mystring = mystring.replace(/sentence/,'string');

var matches = mystring.match(/\b\w+\b/);

That should provide you with essentially the same functionality as preg_match().

So to do the same validation as the preg_match in your question:

if(!phone.val().match(/^[0-9]{3}-|\s[0-9]{3}-|\s[0-9]{4}$/)) {
     phone.addClass("needsfilled");
     phone.val(phonerror);
}

If you absolutely insist on having a function called preg_match() which works exactly like the PHP version, there is a project called PHPJS, which aims to implement PHP functions in Javascript. A quick look at their site shows that although they don't currently have a fully working preg_match() implementation, they have an unfinished implementation which you may want to look at. But frankly, the built-in Javascript .match() function should be more than sufficient.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • with this code if i enter any wrong phone number or both number and text then i don't get any error message something i don't want – Mary May 17 '11 at 13:53
  • @Mary - I hadn't put the `!` at the front of the expression, so it was going into the `if()` when the expression matched rather than when it didn't. But it was intended as example code on how to use the `.match()` function, not a complete copy+paste solution. But I've edited the answer to be closer to what you want to do. – Spudley May 17 '11 at 13:57
  • 1
    @Mary - by the way, just so you know, your phone number regex will fail for anyone outside the US. Not sure about what your site is being used for, but if you're expecting any foreign users, you're going to need to make your regex a lot more lenient. – Spudley May 17 '11 at 13:58
  • [reference for the javascript match function](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/match) – gloomy.penguin Apr 09 '13 at 16:36
3

To use a PHP function, you will have to make an HTTP request to the server (with whatever data you want to test) and then parse the response.

Better to use JavaScript's native regular expression functionality.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

RegExp.exec(string) will work.

try this:

var pattern= "/your pattern/";
var result = pattern.exec ( $("#phone").val() );
// true if match

src: http://javascriptkit.com/javatutors/redev3.shtml

seoul
  • 864
  • 1
  • 12
  • 32
0

Instead of using preg_match, which is a PHP function, you should use the String object's match function. See the Mozilla docs for information on using the match function.

Zach Rattner
  • 20,745
  • 9
  • 59
  • 82