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!

Need to check for valid email in VB script ASP 4

Status
Not open for further replies.

bryant89

Programmer
Nov 23, 2000
150
CA
I know how to check for a valid email in javascript. But I am not sure how to go about it with vbscript in asp. Is there a substr() function in VB also

Any help would be greatly appreciated
 
What do you mean "check for a valid email"?

I think the command that your looking for is mid(). Check Access help for the syntax.

Mark
 
Well I looked in the Interdev help and through a few functions together

I came up with a working vb function that is this:

called from here:

strMsg=strMsg & checkEmail()
------------------------------------------------------------

function checkEmail()

dim strMsg,strDot,strAt,strEmail
strDot="."
strAt="@"
strEmail=txtEmail.value
strMsg=""
if (Instr(strEmail,strAt)=0) or ((left((right(strEmail,4)),1)) <> strDot )then
strMsg=&quot;Email is invalid &quot;
end if
checkEmail=strMsg

end function
 
Wouldn't it be a more elegant solution to validate the email typed into the textbox at the client side using Javascript?

This would mean the user did not have to wait for the server to spit a page back to them announcing they have to reinput their email.
 
Here is a little server side script that works great..

Tim

'All error checking happens on this script, but if it's wrong, we redirect
'the user to a script called &quot;formError.asp&quot; and we pass along the error
'type by appending to the query string what the error was..
'
'
'-- Check for valid email address
UserMail = Trim(Request(&quot;email&quot;))
Loc = InStr(1, UserMail, &quot;@&quot;) 'Location of &quot;@&quot;

If Mid(UserMail, Loc + 1, 1) = &quot;.&quot; Then
'String can't have &quot;.&quot; immediately following &quot;@&quot;
isMail = False

'Elseif InStr(1, UserMail, &quot;@&quot;) = False Then
isMail = False

'Elseif InStr(1, UserMail, &quot;.&quot;) = False Then
isMail = False

'ElseIf InStr(1, Right(UserMail, 2), &quot;.&quot;) > 0 Then
'String must have at least a two-character top-level domain.
isMail = False

'Elseif len(UserMail) < 7 Then
'The email can't be shorter than x@x.com which is 7 characters
isMail = False
Else


'Check to insure they inputed their name
If Trim(Request(&quot;name&quot;)) = &quot;&quot; or isNull(trim(Request(&quot;name&quot;))) Then
isName = False
Else
End if

'Let's send them there if they messed up..

If isName = False Then
Response.Redirect &quot;formError.asp?Name=False&quot;
else


if isMail = False Then
Response.Redirect &quot;formError.asp?Mail=False&quot;
else

 
thanks Tim...
you have more extensive error checking than I do. But that will work great thanks for the extra's

Bryant
 
Why does it matter where the script is run? Is it really gonna take that much time to run it on the server rather than the client?....
 
If all you're doing is checking to see if there's an &quot;@&quot; in the middle of the string and a dot after it with at least one character on either side, it would be better done on the client-side generally. You could run the Javascript function and check the input as soon as the input box loses the focus -- that way the user CAN'T submit a faulty entry.

It's not that the client can process a script faster, it's that just the process of making another HTTP request and getting an answer is too slow.

Now if you're going to do it on the server side, you could get more fancy and it would be worth it. You could run a check over the network and see if there is a running SMTP server at the domain they entered. THAT would make a server-side script worth doing! I've seen an ASP function before to do that -- if I can find it again, I'll post it.
 
oh, yes, mr Bryant89! It makes a difference!

Client-side Javascript will be invoked immediately from the onClick or onSubmit() events of the form or the submit button and it will either allow for submission or it will point out mistakes. Not a single bit of info needs to be exchanged throught the 'Net!

If you process it in Server-side it means you are doing something like this
<form action=&quot;handler.asp&quot;>
...
</form>
So when the user clicks submit button your browser has to fetch the whole of handler.asp. There in this handler.asp you have that isValidEMail() function and you'll check for valid e-mail, and if it's not good, you have to send the original asp file back again. It really will be a lot more time consuming, because it all goes back and forth through the Web. ---
---
 
Oh well I dont want to change it now. I would have to change a lot of pages. Its something I need to think about in round 2.

Hey matman that would be great if you could find that script. That would be interesting
 
Personnally I prefer javascript but in this case I am taking over a project from someone else and that is how they have done it so far, is with server side vbscript. I find javascript a neater way of doing it also. Some of the other error checking I do on the same page needs to query the database which requires the data to be sent from the client to the server so I might as well do all of my error checking the same right?
 
1) The only valid reason to use Server-Side checking is to insure that people who have the JavaScript turned off (around 1-2% of all surfers) also could correctly work with your Web application.

2) Of course, if you need to check something with the database, this kind of error-checking can only be done (in most cases) on the Server-side.

---
---
 
>'Elseif len(UserMail) < 7 Then
'The email can't be shorter than x@x.com which is 7 characters

what about x@x.ca
 
RegExp is a wonderful tool to validate strings. Unfortunately it's not as easy to understand as the VBScript above.
Dim regEx, retVal
Set regEx = New RegExp
regEx.Pattern =&quot;^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$&quot;
regEx.IgnoreCase = true
retVal = regEx.Test(sEmail)

This one is even better:
search for validate.
 
thanks for the info guys
I will let you know if I run into anymore probs
 
hy guys, I see you are lookin' for some client-side script to validate an email address. Here is a part of a solution I took:
function validate(){
var str,strpos=&quot;&quot;;
str=new String();
str=mailform.email.value; //the email address
strpos=str.search('@');
// other validation
// other validation
return(strpos);
}
using the different strpos you can make all validations you want eg: if strpos==-1 then the character (@ or .) is not found and so on

Hope this help.bye
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top