Results 1 to 9 of 9
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Lightbulb UK Phone Number regex validation?

    Can anyone good at regex show me how to put all of these regex validations into 1 please?

    Java Code:
    //don't allow country code
    String regex1 = "/^(\+)[\s]*(.*)$/";
    
    //check all chars are digits
    String regex2 = "/^[0-9]{10,11}$/";
    
    //check first digit starts with 0
    String regex3 = "/^0[0-9]{9,10}$/";
    
    //check that the number is appropriate
    String regex4 = "/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/";

    thanks

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Something like this?

    ^\s*\(?(020[7,8]{1}\)?[ ]?[1-9]{1}[0-9{2}[ ]?[0-9]{4})|(0[1-8]{1}[0-9]{3}\)?[ ]?[1-9]{1}[0-9]{2}[ ]?[0-9]{3})\s*$

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    If the number is start with zero then anyway no need to worry about the + sign at the beginning. Because zero at the first place means + sign omitted anyway.

    Only the digits can validate with a simple rage [0-9]

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    hi Eranga, thanks for the reply. i was hoping you'd also explain it a bit to me! i've broken up your regex pattern to try to understand it but i don't understand parts of it. this is what i've got:

    Java Code:
    ^\s*
    begins with a whitespace character, zero or several times
    
    \(?
    no or exactly 1 open-bracket
    
    Section A:
    -------------------------------
    (020[7,8]{1}\)?
    contains '020'
    followed by -- 7 or 8 (what does comma do??) { ONE OCCURENCE }
    followed by zero or one close-bracket
    
    []?
    i dont understand this part
    
    [1-9]{1}
    any digit 1-9 once
    
    [0-9]{2}
    any digit 0-9 twice
    
    []?
    again, don't understand
    
    [0-9]{4})
    any digit 4 times
    ------------------------------------
    
    |
    must match either Section A OR Section B
    
    
    Section B
    -----------------------------------------
    (0[1-8]{1}
    zero followed by any digit 1-8 ONCE
    
    [0-9]{3}
    any digit, thrice
    
    \)?
    optional close-bracket, why??
    
    []?
    lol, really dont get it
    
    [1-9]{1}
    any digit 1-9 once
    
    [0-9]{2}
    any digit 0-9 twice
    
    []?
    someone please tell me what this is!
    
    [0-9]{3})
    any digit, thrice
    
    \s*$
    ends with any number of white-spaces
    -----------------------------------------

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    i've also come up with this:
    Java Code:
    ^(0[12357])[0-9]{9}$
    starts with 0 followed by one of the following: 1,2,3,5,7
    the rest is any digit 0-9, total 11 digits only

    (apparently 10 digits is extremely rare so i'm not that bothered about 10 digit numbers now)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Range and number define with [0-9]{3}, any digit from zero to nine and only 3 digits can be used.

    If you define []? collectively it has no meaning. Separate them into two [] and ?, then only it gives a meaning. ? sign matches the preceding character 0 or 1 times only. [] matches anything inside the brackets for 1 character position once and only once.

    | is the pipe. As you said pipe matches either side. But you can use it powerfully with grouping. For example abcd(ef|gh) matches both abcdef and abcdgh.

    No need of really use white-space validations. But this regex I've written for a selection on a paragraph. So there could be white-spaces, leading and trailing.

    Have I missed anything?

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by ozzyman View Post
    i've also come up with this:
    Java Code:
    ^(0[12357])[0-9]{9}$
    starts with 0 followed by one of the following: 1,2,3,5,7
    the rest is any digit 0-9, total 11 digits only

    (apparently 10 digits is extremely rare so i'm not that bothered about 10 digit numbers now)
    Seems this is fine. As I said no need to worry about the country code if you are validating initial pattern.

    Is this validate with all your numbers you've. Since I'm not in UK I don't have a better understanding about the number patterns.

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    yeah its okay really because in my system the user is not inputting the data its the staff so i dont really need validation but i thought it'd be better to add some anyway. they made us think like that when i was doing Computing at college.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Phone number stringtokenizer problem
    By jacques5309 in forum New To Java
    Replies: 10
    Last Post: 11-14-2011, 02:12 AM
  2. Phone number Program ..
    By Sary in forum New To Java
    Replies: 9
    Last Post: 03-17-2010, 07:15 PM
  3. Check validation of the Regex
    By itaipee in forum New To Java
    Replies: 4
    Last Post: 05-26-2009, 11:23 AM
  4. GUI Phone
    By nanna in forum New To Java
    Replies: 10
    Last Post: 02-07-2009, 01:15 AM
  5. Menu on phone number option of a list
    By Poonam in forum CLDC and MIDP
    Replies: 7
    Last Post: 01-31-2008, 01:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •