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
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="Email is invalid "
end if
checkEmail=strMsg
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 "formError.asp" 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("email")
Loc = InStr(1, UserMail, "@" 'Location of "@"
If Mid(UserMail, Loc + 1, 1) = "." Then
'String can't have "." immediately following "@"
isMail = False
'Elseif InStr(1, UserMail, "@" = False Then
isMail = False
'Elseif InStr(1, UserMail, "." = False Then
isMail = False
'ElseIf InStr(1, Right(UserMail, 2), "." > 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("name") = "" or isNull(trim(Request("name")) Then
isName = False
Else
End if
'Let's send them there if they messed up..
If isName = False Then
Response.Redirect "formError.asp?Name=False"
else
if isMail = False Then
Response.Redirect "formError.asp?Mail=False"
else
If all you're doing is checking to see if there's an "@" 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.
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="handler.asp">
...
</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. ---
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.
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 ="^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$"
regEx.IgnoreCase = true
retVal = regEx.Test(sEmail)
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="";
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.