Results 1 to 9 of 9
-
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
- 04-07-2011, 06:44 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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*$
- 04-07-2011, 06:46 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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]
-
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 -----------------------------------------
-
i've also come up with this:
starts with 0 followed by one of the following: 1,2,3,5,7Java Code:^(0[12357])[0-9]{9}$
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)
- 04-08-2011, 08:14 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 04-08-2011, 08:15 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
-
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.
- 04-12-2011, 09:15 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Building up those kins of regex, better to try as much as generalize. So you can use them in latter case with minimum effort.
Similar Threads
-
Phone number stringtokenizer problem
By jacques5309 in forum New To JavaReplies: 10Last Post: 11-14-2011, 02:12 AM -
Phone number Program ..
By Sary in forum New To JavaReplies: 9Last Post: 03-17-2010, 07:15 PM -
Check validation of the Regex
By itaipee in forum New To JavaReplies: 4Last Post: 05-26-2009, 11:23 AM -
GUI Phone
By nanna in forum New To JavaReplies: 10Last Post: 02-07-2009, 01:15 AM -
Menu on phone number option of a list
By Poonam in forum CLDC and MIDPReplies: 7Last Post: 01-31-2008, 01:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks