Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

regex for phone number ?  RSS feed

 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need to validate a phone number so i decided using regex this is my code so far.

when i type in 10 numbers i get the error message "Phone Number must be 11 numbers." but when i type for example 12345678909 which is 11 numbers but starts with a 1 not a zero it lets it be added.
before i wrote this code i had it as if (a.length() == 11) so i dont know if its stull trying to use that code or what :/

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A phone number of which country? Including the prefix or just the local number? Are international prefixes optionally accepted? Are you prepared to filter out hyphens, slashes, spaces and other characters that people use routinely in phone numbers?

I'm asking these questions in order to make you aware that there is a lot more to validating phone numbers than you seem to be considering. Providing a user-friendly interface would have to consider these questions.
 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at the moment im not to bothered how advance the regex is i just wanted it to start with zero followed by either 1,2,3,5 or 7 then any 9 numbers i know this isnt good validation as some uk house numbers have 10 digits but im trying to just get it to work for now then advance it later.

But if you know a regex for UK Mobiles and UK home phones that would be great.
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use something like this 0\d{9}
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Over-restrictive syntax checking on form fields is one of my pet hates! Top of my list is credit card numbers where I would like to group the digits in the same way as they appear on my card (typically 4 at a time) with spaces being the group separator but so many sites insist on there being no spaces; spaces are trivial to remove so why disallow them? Next on my list is post/zip codes where it is often assumed that we all have US zip codes and even when UK post codes are allowed we can't put a space in the middle. Coming an honourable third - telephone numbers; I see absolutely no point not allowing grouping using spaces or to allow the international dial code to be prefixed in brackets.

OK - I'm a grumpy old man but I bet there are a lot more like me.
 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even starting with a 1 using that it still accepts it I have a feeling it might be calling the old class when "if (a.length() == 11)" :/ but i dont know how i could find out ifits doing this.
 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just quoted out the code which showed thay optionpane and it still showed up if i only had like 5 numbers soo it must be calling a different class :/
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need this regex? Is it for an exercise where 01234567890 is permitted and you don’t need anything else? Is it for real‑life use? I googled and found this, but you need to check it carefully. I notice the same author’s regex for post‑2001 British registration numbers is dubious, since it does not allow for numbers beginning J T or X, all of which I have seen.
 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its for an assignment for my apprenticeship with QA. To be honest i could probably just leave it out but I just have spair time to add it in and i wanted to have a look at regex soo thought i may aswell add it.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jordan ryan wrote:its for an assignment for my apprenticeship with QA. To be honest i could probably just leave it out but I just have spair time to add it in and i wanted to have a look at regex soo thought i may aswell add it.

Well, one possibility is to separate the business of pattern matching from that of checking the length; especially since String already has a length() method. Then all you'd need is something like "0[0-9]+".

And if you want to allow specific "grouping" characters, just remove them before you check the string, viz:
phoneNumber.replaceAll("[ ()-]", "")

Winston
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
0[1-9][0-9]+ more likely because British phone numbers don’s usually being with 00.
 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at the moment i dont actually think there is a problem with my regex i think there is something wrong with my code as if i comment out the optionpane i still get it no matter what does anyone have any idea what it could be. its practically still running on the old code :/
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jordan ryan wrote:at the moment i dont actually think there is a problem with my regex i think there is something wrong with my code as if i comment out the optionpane i still get it no matter what does anyone have any idea what it could be. its practically still running on the old code :/

Maybe, but don't discount good advice.

Regexes are useful and powerful; but they're not a panacaea. Specifically: they're slow (certainly slower than length()) and arcane.

Winston
 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yer i agree but even when i change it comment out the section it still used the old code so im guessing at the moment the code im thinking is running is not im guessing i have a mix up with classes somewhere or my workspace.
 
jordan ryan
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey i got it work it was a problem with my workspace.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!