0

i am bit week in regex.i am trying to validate phone no by regex & jquery and it works when i am giving phone number like +4401634687222 or 3301634687222. basically phone number may look like below and those no are valid.

+44 (0) 1634 687222
+44(0)1634687222
4401634687222
25805487

here is my code for validation.

var regEx = '1?\W*([2-9][0-8][0-9])\W*([2-9][0-9]{2})\W*([0-9]{4})(\se?x?t?(\d*))?';
var val = jQuery.trim($("input[id*='txtEnqphone']").val())
if (!val.match(regEx)) {
    if ($('#Enquery_feed_loader').html() != '') {
        $('#Enquery_feed_loader').fadeOut('slow', function () {
            $('#Enquery_feed_loader').html('<span>' + dialog_Msg.Invalid_Phone + '</span>');
        }).fadeIn('slow');
    } else {
        $('#Enquery_feed_loader').fadeOut('slow', function () {
            $('#Enquery_feed_loader').html('<span>' + dialog_Msg.Invalid_Phone + '</span>');
        }).fadeIn('slow');
    }

    $("input[id*='txtEnqphone']").focus()
    flag = false;
    return false;
}

so just change my regex expression as a result i can enter the above phone number. thanks

ebram khalil
  • 8,252
  • 7
  • 42
  • 60
Thomas
  • 33,544
  • 126
  • 357
  • 626

2 Answers2

0

Try this :

^\(?\d{3}\)?[- ]?\d{3}[- ]?\d{4}$
Moeed Farooqui
  • 3,604
  • 1
  • 18
  • 23
0

try this :

^[+][0-9]\d{2}-\d{3}-\d{4}$

The above regex will allow following pattern :

+974-584-5656 | +000-000-0000 | +323-343-3453

And following will not be allowed.

974-584-5656 | +974 000 0000

Edit :

For above regex modification is : ^[0-9]\d{2} \d{3} \d{4}

The other regex you can try out is :

^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$

Allowed : 2405525009 | 1(240) 652-5009

PVR
  • 2,534
  • 18
  • 38
  • people may give without + sign also and that should be allowed. so what to change in regex for that.plus sign should be optional. people may use or may not. – Thomas Jun 27 '13 at 11:57
  • i want the phone no may or may not start with + sign.phone no may have - or () chanracter or may have space and length should be more than 6. can u please give me the exact regex for my requirement. thanks – Thomas Jun 27 '13 at 12:15
  • you have to play a bit around this !!! – PVR Jun 27 '13 at 12:29