Check if a string is a mail adress

Mahares

Expert
Licensed User
Longtime User
Would this work for you or is it too simplistic:

B4X:
Dim eAddress As String
Dim Suf As String   :Suf=".net"     'or .com or dot whatever

'BELOW TO VERIFY IF EMAIL ADDRESS IS VALID
   eAddress="[email protected]"
   If eAddress.Contains("@") AND eAddress.EndsWith(Suf) Then
      Msgbox("Valid email: " & eAddress,"")
   Else
      Msgbox("Not a valid email address.","")
   End If
 
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
Would this work for you or is it too simplistic:

B4X:
Dim eAddress As String
Dim Suf As String   :Suf=".net"     'or .com or dot whatever

'BELOW TO VERIFY IF EMAIL ADDRESS IS VALID
   eAddress="[email protected]"
   If eAddress.Contains("@") AND eAddress.EndsWith(Suf) Then
      Msgbox("Valid email: " & eAddress,"")
   Else
      Msgbox("Not a valid email address.","")
   End If

I have tried it no it `s works ok But i have to test com.no.net. I tried several things to add all off them but i dont get itt to work
whith more and one at the time do you know a way to do that?
 
Last edited:
Upvote 0

BarrySumpter

Active Member
Licensed User
Longtime User
RegEx

In PHP I use a string like '/^[_a-z0-9!#$%&~\{\}\-]{1,64}(\.[_a-z0-9!#$%&~\{\}\-]{1,64})*@[_a-z0-9\-]{1,63}(\.[_a-z0-9\-]{1,63})*(\.[a-z]{2,6})$/i'

I haven't tried converting it to work in B4A yet, but it works well in PHP.

Can someone please translate that php string?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Sorry Barry. I do not know php, but here is another sophisticated way to check if a string is an email address. It is a little fancier than the first one I put in the previous post:
B4X:
'BELOW TO CHECK IF A STRING IS AN EMAIL ADDRESS
      Dim eAddress As String
      Dim eMatch As Matcher
        eAddress="[email protected]"
      eMatch = Regex.Matcher("\w+@\w+\.\w+", eAddress)
      If eMatch.Find = True Then
           Msgbox(eAddress & " is a valid email address","")
      Else
           Msgbox( eAddress & " is not a valid email address.","")   
      End If
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Check this code:
B4X:
Dim Valid As Boolean

Valid = Regex.IsMatch("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", "[email protected]")
            
If Valid Then
            
   Msgbox("Valid Email Address", "")
                     
Else
            
   Msgbox("Invalid Email Address", "")
                     
End If
 
Upvote 0

volvomann

Active Member
Licensed User
Longtime User
Sorry Barry. I do not know php, but here is another sophisticated way to check if a string is an email address. It is a little fancier than the first one I put in the previous post:
B4X:
'BELOW TO CHECK IF A STRING IS AN EMAIL ADDRESS
      Dim eAddress As String
      Dim eMatch As Matcher
        eAddress="[email protected]"
      eMatch = Regex.Matcher("\w+@\w+\.\w+", eAddress)
      If eMatch.Find = True Then
           Msgbox(eAddress & " is a valid email address","")
      Else
           Msgbox( eAddress & " is not a valid email address.","")   
      End If
I use this it work almost as o want:sign0098:
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Yes, it is an alien language and while most languages keep pretty much to the standards there are differences. To translate it a bit-

'/^[_a-z0-9!#$%&~\{\}\-]{1,64}(\.[_a-z0-9!#$%&~\{\}\-]{1,64})*@[_a-z0-9\-]{1,63}(\.[_a-z0-9\-]{1,63})*(\.[a-z]{2,6})$/i'

The / at beginning and ends just sort of mark the string like quotes around a string variable. The i at the end ignores character case (upper/lower). The ^ represents the beginning of the string and $ the end, so all that they send must meet the format (if just ^ was used it would mean what they give starts with the format, etc). The [] areas represent chars and char ranges in that section of the format string with some special chars needing \ escaped. The {min, max} sections show how many characters from that group. The () just group sections further and sometimes can have other meaning or allow modifying a found group/section later. The () sections here are just used to further specify a count with the * which is 0 or more occurrences since not all emails contain a dot, but it is allowed. I used to have the last part of the string as (\.[a-z]{2,3}) which was for the top level domains like .net, .com, .edu, .au, .de, etc. There are some crazy domains lately though that can be up to like 11 chars, but I allowed for 2-6 in length to get the ones I care about and not leave it so open.
 
Upvote 0

CapReed

Member
Licensed User
Longtime User
Hello!

Thank you very much for this code. It really looks like a crazy language ... imagine what they think of us in a few years! :confused:

The code is almost perfect, but when there are multiple parties domains, validation is not correct.

For example for email <[email protected]> the result is not correct.

Just so you have in mind.

Best Regards.
 
Upvote 0

ukimiku

Active Member
Licensed User
Longtime User
Even Chinese characters (along with German umlauts, French accents...) are already possible in an effective e-mail address (in the domain part). Chinese characters in the address part (to the left of the "@" character) are to follow suit soon:

IEFT planning internationalised email addresses - The H: Open Source, Security and Development

The last paragraph states:
"[...] experts believe that Chinese registrars in China and Taiwan will quickly implement the change for internationalised email. Representatives of CNIC and TWNIC are authors of the standards. Chinese users currently have to write emails in ASCII to the left of the @ and in Chinese characters to the right of it for Chinese domains, which have already been internationalized."

NOW I would like to see a correct, culturally unbiased validation method for a real-life e-mail address :confused:

Regards,
 
Upvote 0

BarrySumpter

Active Member
Licensed User
Longtime User
Reposted reformat to make an easier read ( for me ;) )

Thanks Roger for taking the time for that verbose response.


Yes, it is an alien language and while most languages keep pretty much to the standards there are differences. To translate it a bit-

'/^[_a-z0-9!#$%&~\{\}\-]{1,64}(\.[_a-z0-9!#$%&~\{\}\-]{1,64})*@[_a-z0-9\-]{1,63}(\.[_a-z0-9\-]{1,63})*(\.[a-z]{2,6})$/i'

The / at beginning and ends just sort of mark the string like quotes around a string variable.

The i at the end ignores character case (upper/lower).

The ^ represents the beginning of the string and $ the end, so all that they send must meet the format
(if just ^ was used it would mean what they give starts with the format, etc).

The [] areas represent chars and char ranges in that section of the format string with some special chars needing \ escaped.

The {min, max} sections show how many characters from that group.

The () just group sections further and sometimes can have other meaning or allow modifying a found group/section later.

The () sections here are just used to further specify a count with the * which is 0 or more occurrences since not all emails contain a dot, but it is allowed.

I used to have the last part of the string as (\.[a-z]{2,3}) which was for the top level domains like .net, .com, .edu, .au, .de, etc.

There are some crazy domains lately though that can be up to like 11 chars, but I allowed for 2-6 in length to get the ones I care about and not leave it so open.
 
Upvote 0

Roger Garstang

Well-Known Member
Licensed User
Longtime User
My PHP script works with dotted domains. You'd have to convert it letter by letter exact to B4A to get best results. All the foreign chars will start to get nuts in regex. There are some word level commands, but I think it still uses the PHP language/country. Best way to really verify would be with an SMTP connection. I had started looking at that anyway to make a way to directly email from PHP without PHP being installed on a mail server to email. There are some cool apps that make direct connections I like too. More secure with the downside of no caching by middle man to retry though. Lots of spammers use it to screw up the headers so you can't trace them, so it is starting to get filtered though.
 
Upvote 0
Top