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 :/
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.
But if you know a regex for UK Mobiles and UK home phones that would be great.
OK - I'm a grumpy old man but I bet there are a lot more like me.
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("[ ()-]", "")
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.