0

I need a REGEX with following specifications:

  1. Allowed chars:
    (blank space), (, ), +, , 0-9
  2. ( can be first char after trim like (+61) 312 405 678 or +61 312 405 678
  3. Length: min 8 characters max 16 – show error in case of boundary conditions.

4 Answers4

3

I would rather stick with standards and won't reinvent the wheel. You can use a special library for handling phone numbers called libphonenumber

Kris
  • 5,714
  • 2
  • 27
  • 47
1

try this

reg exp: ^(\(\+\d{2}\)|\+\d{2})[ -]?(\d{3}[ -]?){2}\d{3}$
Jayy
  • 2,368
  • 4
  • 24
  • 35
0

Not a duplicate, but this may give some answers. My approach would be to strip out the characters that aren't digits (aside from +) and then process the regex based on that. Should be a lot easier to deal with.

You could also try http://regexlib.com.

Community
  • 1
  • 1
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • i need this regex for validation purpose ! Stripping the characters is a task which i will perform after validation is done. – Hardik Handa Aug 25 '11 at 11:36
  • 1
    @Hardik Handa It's probably better stripping them before validation, that way you can ignore formatting quirks and just accept the number however people type it. Saves for frustrating user experiences trying to match your chosen format of entering phone numbers! – Michael Berry Aug 25 '11 at 11:41
  • well that would upset the synchronicity of the application – Hardik Handa Aug 25 '11 at 12:05
0

According to your specifications, this regex would work:

[-+\d() ]{8,16}

This will also match (--++--), so you may want to change/clarify your specs.

Also, regex do not "show error", the only thing they do is match or not.

Qtax
  • 33,241
  • 9
  • 83
  • 121
  • but we have to also see that if the bracket is opened then it must be closed and that + can only come in first place or in second place if their is an opening bracket !! what ur specified regex does is set minimum and maximum length and allow those special characters rest conditions are totally ignored, thanx for the effort thought friend :)) – Hardik Handa Aug 28 '11 at 15:28