What would be the easiest way to test for valid email address in a text box. I will at least check for '@' and a '.' but what else can i do? What is the best way to test for those characters?
Thanks in advance.
Printable View
What would be the easiest way to test for valid email address in a text box. I will at least check for '@' and a '.' but what else can i do? What is the best way to test for those characters?
Thanks in advance.
Just use InStr to make sure that they are actually in the string (if they aren't, a 0 will be returned). Then, make sure that the @ is at least the second character, and the period is at least one character after the @ and that there is at least two characters after the dot.
VB Code:
Dim strEmail As String strEmail = "[email protected]" If InStr(strEmail, "@") Then If InStr(strEmail, ".") Then MsgBox "Yep, there is a @ and a . " Else MsgBox "There is a @, but no ." End If Else MsgBox "there is no @ so I didn't even se if there was a . in it" End If
Slight modification, checks for the "." after the "@".
VB Code:
Dim strEmail As String Dim iEmail As Integer Dim iPeriod As Integer strEmail = "[email protected]" iEmail = InStr(strEmail, "@") iPeriod = InStr(iEmail, strEmail, ".") If iEmail Then If iPeriod And Len(strEmail) - iPeriod >= 2 Then MsgBox "Yep, there is a @ and a . " Else MsgBox "There is a @, but no . in the domain name" End If Else MsgBox "there is no @ so I didn't even se if there was a . in it" End If
Just in case they enter blah.blah@blahcom
Also makes sure there are at least 2 characters after the ".".
Do you have any other requirements for determining a valid email address?
s.
Thanks guys!! Instr was what i was using in the beginning but i thought there may have been some advancements in validation code, maybe i'll write a textbox control and have it test the string based on the code from silva and carp.
Cheers
:D