M.Svrcek
M.Svrcek

Reputation: 5635

REGEX for phone number

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.

Upvotes: 1

Views: 673

Answers (2)

Toto
Toto

Reputation: 91373

You could use:

var regEx = /^\+\d{3}(?: \d{3}){3}$/;

Upvotes: 0

ohaal
ohaal

Reputation: 5268

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}$

Upvotes: 1

Related Questions