There seems to be a few questions lately on using Regex to validate phone numbers, email, etc. I hope this helps to see you don't need a lot of knowledge of them to use them.
First, I am not a regex expert but Google will find most any answer you need. The thing is you will find plenty that don't work. Just keep trying and don't be afraid of them. If you want to know the details try places like:
Regular Expression Library Note that unless you create them often, it is hard to remember all the syntax and it is far easier to just Google up some solutions.
Second, I haven't needed any in B4A yet but I have used them in VB.Net. Here are a few samples (in VB.Net). Note how some need '/'s and some don't. I hope someone finds them useful.
Private Function IsValidEmail(ByVal strIn As String) As Boolean
' Return true if strIn is in valid e-mail format.
If Not IsNothing(strIn) AndAlso strIn <> "" Then
Return Regex.IsMatch(strIn, ("^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"))
Else
Return False
End If
End Function
Public Function IsValidIP(ByVal Addr As String) As Boolean
Dim Pattern As String = "^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$"
Dim Check As Regex = New Regex(Pattern)
Dim Valid As Boolean = False
If (Addr = "") Then
Valid = False
Else
Valid = Check.IsMatch(Addr, 0)
End If
Return Valid
End Function
Protected Function ValidatePW(ByVal NewPW As String) As Boolean
'At least one upper case, 1 lower case, one digit, one special character.
Return Regex.IsMatch(NewPW, "(?=^.{7,12}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$^&_-]).*$")
End Function
'This one is to help catch SQL Injection attempts
Public Shared Function IsValidClause(ByVal strIn As String) As Boolean
Dim blnReturn = False
If Regex.IsMatch(strIn, ("/((\%3C)|<)[^\n]+((\%3E)|>)/I")) Then 'CSS
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/exec(\s|\+)+(s|x)p\w+/ix ")) Then 'SQL Injection
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))union/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))select/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))drop/ix")) Then
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))delete/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))insert/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))update/ix")) Then
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))create/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))from/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))update/ix")) Then
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))xp_/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))rowset/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))update/ix")) Then
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))declare/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))begin/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))update/ix")) Then
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))end/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))exec/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))update/ix")) Then
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))sp_/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))where/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))update/ix")) Then
blnReturn = False
ElseIf strIn = "" Then
blnReturn = False
Else
blnReturn = True
End If
Return blnReturn
End Function
First, I am not a regex expert but Google will find most any answer you need. The thing is you will find plenty that don't work. Just keep trying and don't be afraid of them. If you want to know the details try places like:
Regular Expression Library Note that unless you create them often, it is hard to remember all the syntax and it is far easier to just Google up some solutions.
Second, I haven't needed any in B4A yet but I have used them in VB.Net. Here are a few samples (in VB.Net). Note how some need '/'s and some don't. I hope someone finds them useful.
Private Function IsValidEmail(ByVal strIn As String) As Boolean
' Return true if strIn is in valid e-mail format.
If Not IsNothing(strIn) AndAlso strIn <> "" Then
Return Regex.IsMatch(strIn, ("^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"))
Else
Return False
End If
End Function
Public Function IsValidIP(ByVal Addr As String) As Boolean
Dim Pattern As String = "^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$"
Dim Check As Regex = New Regex(Pattern)
Dim Valid As Boolean = False
If (Addr = "") Then
Valid = False
Else
Valid = Check.IsMatch(Addr, 0)
End If
Return Valid
End Function
Protected Function ValidatePW(ByVal NewPW As String) As Boolean
'At least one upper case, 1 lower case, one digit, one special character.
Return Regex.IsMatch(NewPW, "(?=^.{7,12}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$^&_-]).*$")
End Function
'This one is to help catch SQL Injection attempts
Public Shared Function IsValidClause(ByVal strIn As String) As Boolean
Dim blnReturn = False
If Regex.IsMatch(strIn, ("/((\%3C)|<)[^\n]+((\%3E)|>)/I")) Then 'CSS
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/exec(\s|\+)+(s|x)p\w+/ix ")) Then 'SQL Injection
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))union/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))select/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))drop/ix")) Then
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))delete/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))insert/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))update/ix")) Then
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))create/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))from/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))update/ix")) Then
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))xp_/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))rowset/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))update/ix")) Then
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))declare/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))begin/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))update/ix")) Then
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))end/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))exec/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))update/ix")) Then
blnReturn = False
ElseIf Regex.IsMatch(strIn, ("/((\%27)|(\'))sp_/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))where/ix")) Or Regex.IsMatch(strIn, ("/((\%27)|(\'))update/ix")) Then
blnReturn = False
ElseIf strIn = "" Then
blnReturn = False
Else
blnReturn = True
End If
Return blnReturn
End Function