[SOLVED] Help with perl regexp for mobile phone number

5 years 11 months ago - 5 years 11 months ago #304137 by paxx
Hi
I'm trying to validate a mobile phone number in the following forms with a regexp (the field is a textfield):
012-345 67 89 or 0123-45 67 89

Iv'e search internet (googled) and had a lot of result but none that worked for me.
Iv'e searched this forum but with no luck in the two ways.

My experience with regexp (and coding) is 0 so it took some hevily reading before I finally came up with this:
/^\d{3}-\d{3} \d{2} \d{2}$/

And this validated the first mobile number (great) but not the second one.
So I change the code to this:
/^\d{3,4}-\d{2,3} \d{2} \d{2}$/

So I'm getting closer, now this validates the first mobile number and the second mobile number unfortunatly it also validates a mobile phone number as correct in the followig way:
012-34 56 78 (which is wrong) and also 0123-456 78 90 (which is also wrong).

How should a change the above regexp so that it only validates in my two cases?
Is there anyone that please could help me out with this.

Regards
Stephen

If the problem can be solved why worry? If the problem cannot be solved, worrying will do you no good.

Please Log in to join the conversation.

5 years 11 months ago #304144 by krileon
Replied by krileon on topic Help with perl regexp for mobile phone number
Sorry, we do not provide custom coding assistance. This includes custom REGEXP. At most we can provide simple examples. Recommend checking out some REGEXP resources below.

stackoverflow.com/questions/16699007/regular-expression-to-match-standard-10-digit-phone-number
www.regexlib.com/Search.aspx?k=phone

As you're trying to validate 2 completely different formats I recommend just using an OR (in REGEX you'd use a | for OR case) case in your REGEXP to handle both. Example as follows based off the REGEX you supplied.

/^((\d{3}-\d{3} \d{2} \d{2})|(\d{3}-\d{3} \d{2} \d{2}))$/


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

5 years 11 months ago #304160 by paxx
Hi
Thanks a lot for pointing out the operator "OR" did not know that there was any like this.

Redone the regexp and it works perfectly well now..

Thank you so much.

And for others the full regexp looks like this
/^((\d{3}-\d{3} \d{2} \d{2})|(\d{4}-\d{2} \d{2} \d{2}))$/
This would validate a phone number in one of either format:
XXX-XXX XX XX or XXXX-XX XX XX

Regards
Stephen

If the problem can be solved why worry? If the problem cannot be solved, worrying will do you no good.
The following user(s) said Thank You: nant, krileon

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.145 seconds

Facebook Twitter LinkedIn