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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using Instr funcion 2

Status
Not open for further replies.

611brain

Programmer
Feb 26, 2007
10
CA
Hi

I want to see if user enter their email address correctly in txtbox or not.. not sure how to check

here my code:

<form name="form" id="form" method="post" action="">
<p><strong>Customer Login:</strong></p>
<p>Username:
<input type=text name=username />
<!-- this name of textbox in form,this is used by the script to assign values to variables -->
</p>



i hardcoded.

email="vbscript@tip.com"


i want to see if user miss @ symbol then give it error
here is my code:

Instr(“vbscript@tip.com”, “@”)

if request("username")<> Instr then

response.write "error"

else

response.write "thank you"
end if

But i am not sure if it right or wrong? how am i going to check if user enter @ or not using instr function?

thanks
 
I think a regular expression may be better. Here is a simple example.

Code:
email = "vbscript@tip.com"

If CheckEmail(email) Then
	WScript.Echo "This is a valid email address."
Else
	WScript.Echo "Please enter a valid address."
End If

Function CheckEmail(strEmailAddr)
' 	On Error Resume Next

	Dim regExp1  :  Set regExp1 = New RegExp
	regExp1.Pattern = "^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$"
	regExp1.IgnoreCase = True
	CheckEmail = regExp1.Test(strEmailAddr)
End Function

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
If you want to siply use the InStr function then you can do it like this:

Code:
email = "vbscript@tip.com"

If Not InStr(email, "@") Then
	WScript.Echo "Please check the email address."
End If

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
If your code was copied and pasted, the "smart" quotes around the values in the Instr function call, you'll have problems.

As well, your code really doesn't mean anything or do anything. The Instr function returns the index of the character substring in the character string, but you haven't stored that in a variable for future reference in your first call to the function.

In the second call to Instr, you don't give it any parameters, and comparing the numeric index to a character string will always return False.

Also, if you want to do client-side error checking, you should use Javascript rather than VBScript because only IE is capable of interpreting VBS code.

Lee

 
[tt]if instr(request("username"),"@")=0 then
response.write "error"
else
response.write "thank you"
end if
[/tt]
I have refrained from saying what I intended to say. Do the forum a favor, read the basic vbs documentation/tutorial.
 
thanks but i want to use instr() function

thanks
 
thanks i will try it out

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top