Is it a phone number ?

SoyEli

Active Member
Licensed User
Longtime User
Hello:

Is it possible to know if a string is a phone number ?
without " isnumeric() " and is 10 digits.
In VB6 = If string Like "(###) ###-####) then ...

Dim PH as string
PH="(555) 123-4567"
PH="5551231234"
PH="555 123 1234"

Thank you :sign0163:
 
Last edited:

SoyEli

Active Member
Licensed User
Longtime User
Thank you for the reply:

I have this, does not work

If Regex.IsMatch("(555) 123-1234","(999) 625-1234") Then
Msgbox("Match","")
Else
Msgbox("NO Match","")
End If

Thank you:
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
I have an tread about check if Emailaddres is i true how can I use regec to check that?

This is a very useful site to find out about regular expressions Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns and I'm pretty certain that there is an example of how to check if a valid email has been entered. I recall using this resource when programming with RegEx in Basic4PPC, it is very well presented and easy to follow.

Regards,
RandomCoder
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Can you give a sample ?

If I get a string With "5551231234" or "(555) 123-1234) how can I know if this
is a phone number ? How to compare to "(###) ###-####"

My appologies if this is incorrect as I'm not very good with regular expressions but I'd try something like this as your expression...
B4X:
\b\d{10}|(\d{3}) \d{3}-\d{4}\b

Through past experience I usually find that my expressions don't work first time and I have to experiment a little to get them to work correctly.

Regards,
RandomCoder
 
Upvote 0

SoyEli

Active Member
Licensed User
Longtime User
\b\d{10}|(\d{3}) \d{3}-\d{4}\b

Thank you for the reply:

This did not work:

But thank you for trying :)
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
This did not work:
But thank you for trying :)

Sorry about that :eek:

Having had some spare time on my hands today so I've tried to work out where it went wrong and I've found out that brackets are classed as a special charater and so they need a backslash to work. This is the correct RegEx expression that you require...
B4X:
\b\d{10}|\(\d{3}\) \d{3}-\d{4}\b
It's tried and tested to work!

Unfortunately I've broken my B4A installation as I was still running on version 1.6 and had an emulator set up for Android 1.6 which is now no longer working. Only wasted around an hour to find out its a known bug http://www.b4x.com/forum/additional-libraries-official-updates/16510-basic4android-v1-90-fix-android-v1-60-a.html#post94025 but this doesn't want to work for me :BangHead: Guess I'll be posting my own question next :confused:

Anyway, attached is a sample for you to play with. If you enter any combinations of 10 numbers or a number squence in the format of (123) 123-1234 it will produce and OK message. If you need to add more patterns this can be done using a | symbol followed by the expression.

Regards,
RandomCoder
 

Attachments

  • RegExTest.zip
    6.8 KB · Views: 185
Upvote 0

IanMc

Well-Known Member
Licensed User
Longtime User
Hey thanks for this and the RegExTest.zip

Would it take much to modify this to also test for international phone number format?

In the UK we don't use brackets in phone numbers (usually) but I would like to have a function that tests to see if a string 'could' be a valid phone number before sending it off to an SMS function.

A UK phone number always begins with zero like:

07951 292534
or
07888 211549

unless entered with the international code preceded by a plus sign which represents two zeros in which case you miss out the leading zero such as
+447951 292534
or
+447888 211549

Could this function be written to only return a string of numbers if they were the minimum length for a phone number and not over the max length for a phone number (if there is one) perhaps replace the + sign with two zeros, then strip out any spaces and brackets and any non numerics (perhaps the minus sign) then test for length and return if within min and max length?

so I could get the string, send it to the function, if the function returns a string then I know it 'could' be a valid phone number something like:

DIM s as String = "+44 (7888) - 211549"
s = CBVPN(s) ' now if s contains anything it 'could' be a valid phone number
and in this case s would now be 00447888211549 so...
IF s <> "" Then GoForIt(s)

So the question is, what's the min length for a phone number dialed over the cellular network and what's the max length?
Would a valid phone number always begin with at least one zero?

Sorry, bit rusty at RegEx :)

Actually this function might be handy for others but I think I will demand that the phone number be entered without any spaces or special characters and that international format numbers start with two zeros :)

Should make life easier :)
 
Upvote 0
Top