Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

email validation 1

Status
Not open for further replies.

mad411

Technical User
Apr 4, 2002
17
IN
hello experts,

is it possible for me to validate the email address typed by the user, i.e vbscript should check for all the things in the add. i.e only one @ sign only one . before com &
.co.in etc
i know this can be done using vbscript but dont know how?
so please can anyone help
 
this should do it

SearchString = email
SearchChar = "@"
MyPos = Instr(1, SearchString, "@")
if MyPos = 0 then

MyVar = MsgBox ("You must fill in a valid E-mail address!")

elseif MyPos = Instr(1, SearchString, "@") then

Hope that helps provide tools to let people become their best.
 
hi onpnt,

thank you very much for your reply :->
but the code u sent checks only for @ sign & not for .com or .co.in :-(
i need that vbscript should check for proper email address validation
ok i'll try to explain again
i have a text box & a submit button in my .HTML file
the user inputs his email address & vbscript checks if it is valid or not
(for ex: xyz@yahoo.co.in or xyz@yahoo.com are valid email addresses & xyz@yahoo.co or xy@z@yahoo.com. is invalid right)
i want my vbscript code to do this validation

any suggestions will be helpful
thank you


[noevil]
 
I never needed to try this but all you should need to follow up with is this condition then to search for the possibility of two @@ char's. or do you actually want to search for .com or .co.in.? doesn't really make sense to me to do that though.

SearchString = email
SearchChar = "@"
MyPos = Instr(1, SearchString, "@")
if MyPos = 0 then
MyVar = MsgBox ("You must fill in a valid E-mail address!")

elseif MyPos = 2 then
MyVar = MsgBox ("You must fill in a valid E-mail address!")

elseif MyPos = Instr(1, SearchString, "@") then
[whatever is next] provide tools to let people become their best.
 
can't believe I thought that would work. I wrote this real fast and dirty but it should help you out
<SCRIPT language=&quot;VBscript&quot;>

sub validate_OnClick(MyVar)
Dim email
email = 0

If Len(items.email.value) <= 5 Then
email = 1
End If

If InStr(1, items.email.value, &quot;@&quot;, 1) < 2 Then
email = 1
Else
If InStr(1,items.email.value, &quot;.&quot;, 1) < 4 Then
email = 1
End If
End If

If email <> 0 then
MyVar = MsgBox (&quot;You must fill in a valid E-mail address!&quot;)
End If
end sub
</script> provide tools to let people become their best.
 
if instr(len(mystring) - 7), mystring, &quot;.&quot;) > 0 then response.write &quot;Its there&quot;.

7 wasn't chosen arbitratily. It's 1 character longer than the longest top level domain (.museum).
 
hey
thanx to u all

i've been to the site


where i found this asp code for email validation


<html>
<head>
<title>VBscript email validation</title>
</head>
<body>
<%
Dim emailAddress
emailAddress = Request(&quot;emailAddress&quot;)
if emailAddress <> &quot;&quot; then
emailAddress = Cstr(emailAddress)
if emailAddress <> &quot;&quot; then
blnValidEmail = RegExpTest(emailAddress)
if blnValidEmail then
Response.Write(&quot;Valid email address&quot;)
else
Response.Write(&quot;Not a valid email address&quot;)
end if
end if

Function RegExpTest(sEmail)
RegExpTest = false
Dim regEx, retVal
Set regEx = New RegExp

' Create regular expression:
regEx.Pattern =&quot;^[\w]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$&quot;

' Set pattern:
regEx.IgnoreCase = true

' Set case sensitivity.
retVal = regEx.Test(sEmail)

' Execute the search test.
If not retVal Then
exit function
End If

RegExpTest = true
End Function
Else
%>

<form>
<input type=&quot;text&quot; name=&quot;emailAddress&quot;>
<input type=&quot;submit&quot; value=&quot;submit&quot;>
</form>

<%End If %>

</body>
</html>

well this code runs well in .asp file [2thumbsup]
i modified this code so that it can be used in .html file
the code i modified is as follows


<html>
<head>
<title>VBscript email validation</title>
</head>
<body>
<script language=&quot;vbscript&quot;>
<!--
Sub btn_reg_onclick
Dim emailAdd
emailAdd = form1.emailAddress
if emailAdd <> &quot;&quot; then
emailAdd = Cstr(emailAdd)
if emailAdd <> &quot;&quot; then
blnValidEmail = RegExpTest(emailAdd)
if blnValidEmail then
alert &quot;Valid email address&quot;
else
alert &quot;Not a valid email address&quot;
end if
end if
RegExpTest(sEmail)
Else
msgbox &quot;hi&quot;
End If
End Sub

Function RegExpTest(sEmail)
RegExpTest = false
Dim regEx, retVal
Set regEx = New RegExp

' Create regular expression:
regEx.Pattern =&quot;^[\w]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$&quot;

' Set pattern:
regEx.IgnoreCase = true

' Set case sensitivity.
retVal = regEx.Test(sEmail)

' Execute the search test.
If not retVal Then
exit function
End If

RegExpTest = true
End Function
-->
</script>
<form name=&quot;form1&quot;>
Enter e-Mail Address<input type=&quot;text&quot; name=&quot;emailAddress&quot;><br>
<input name=&quot;btn_reg&quot; type=&quot;submit&quot; value=&quot;Click To Register&quot;>
</form>
</body>
</html>



when i run the above .html file & if i enter invalid email
address it shows proper alert message but if i enter a valid email address then also it shows the same message i.e &quot;not a valid email address&quot; [sad] why is that happening??
also it is not giving the &quot;hi&quot; message


can u help me out with this code please
i know that there is a mistake in the code but i cant figure out what [sadeyes]




[noevil]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top