1

I'm trying to create regex match for phone number in jquery, for exactly this format

+XXX XXX XXX XXX, where X is digit.

I created this

var regEx = '[+]{1}[\d]{3}[ ]{1}[\d]{3}[ ]{1}[\d]{3}[ ]{1}[\d]{3}';
        var val = jQuery.trim($('#phone_number').val())
        if (val.match(regEx)) {
            alert('good');
        }
        else
            alert('bad');
        }

Any assistance will be helpful.

Balaji Kandasamy
  • 4,446
  • 10
  • 40
  • 58
M.Svrcek
  • 5,485
  • 4
  • 25
  • 33
  • please look at the link. it has almost all regex combinations. http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation?rq=1 – Sp0T Feb 04 '14 at 11:32
  • i dont want all of them..i want exactly this one – M.Svrcek Feb 04 '14 at 11:35

2 Answers2

1

You almost have it, but there are a few "unnecessaries", and you're missing anchors assuming the phone number is not part of a larger string (and this is for validation).

^\+\d{3} \d{3} \d{3} \d{3}$
ohaal
  • 5,208
  • 2
  • 34
  • 53
0

You could use:

var regEx = /^\+\d{3}(?: \d{3}){3}$/;
Toto
  • 89,455
  • 62
  • 89
  • 125