-1

I need the pattern checking for mobile number in(+971) 556-123456 this format in HTML phone numbers.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sasi Rekha
  • 59
  • 6

1 Answers1

2

I think this is the RegEx you need:

\(\+971\) [0-9]{3}-[0-9]{6}

Here's a live DEMO of it.

And If I am not wrong the +971 prefix is optional so the right RegEx would be:

(\+971)? [0-9]{3}-[0-9]{6}

And this is a live DEMO of it.

EDIT:

If the +971 prefix is not optional and I assume the parentheses are optional, here's the RegEx you need:

\+971\s[0-9]{3}-[0-9]{6}

Make sure you add the \s to ensure having the space after the +971 prefix.

This is a Working DEMO and you can test it in the above snippet:

<form action="demo_form.asp">
  Phone number:
  <input class="form-control" pattern="\+971\s[0-9]{3}-[0-9]{6}" name="homeNo" value="+971 555-4144" id="homeNo" required />

  <input type="submit">
</form>
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • 971 is not optional and pattern is not working in jsp. pattern = "\(\+971)? [0-9]{3}-[0-9]{6}" maxlength="8" . but pattern is not working. Thanks for your response – Sasi Rekha Jul 15 '15 at 05:38
  • How it doesn't work? What do you get? And if the 971 is not optional you can take a look at my EDIT. – cнŝdk Jul 15 '15 at 07:41
  • I am using in HTML5. if I gave wrong format input, while I am submitting the form, I will get error message. but I am able to submit the form.This is my html 5 code . $(document).ready(function() { $("#homeNo").jqxMaskedInput({ width: '100%', height: '100%', mask: '(+971) 5##-######'}); $('#mobileNo').attr("style","");}); – Sasi Rekha Jul 15 '15 at 08:28
  • please post your HTML5 code in an EDIT to the question. – cнŝdk Jul 15 '15 at 08:50
  • Here's the HTML code for it: `Phone number:
    ` I tested it and it works perfectly, take a look at the snippet in my EDIT.
    – cнŝdk Jul 15 '15 at 09:06
  • yes. its working fine in your code. but I don't know why, its not working in my code. I am using jqwidgets, using that I am using some masking as well.I am not sure, that could be issue. see my code above.Thanks for your response. – Sasi Rekha Jul 16 '15 at 05:56
  • Ok I see, but what are you getting in console? does it show any error? – cнŝdk Jul 16 '15 at 07:39
  • No. I am not getting any errors in console. – Sasi Rekha Jul 20 '15 at 06:12
  • Maybe you browser doesn't support HTML5 pattern? – cнŝdk Jul 20 '15 at 07:10
  • its working fine with the below code: I just removed onvalid and oninvalid functions and its working fine now. Thanks for your effort. Thanks a ton. – Sasi Rekha Jul 20 '15 at 08:32