Validate a email address

I am using ASPMail to send mail.  It works fine, and email is sent, but only if the email is valid.  Otherwise it doesn't.  However, no mater what, the Thankyou page comes up, just because the the email field is not empty.
   I'm looking for a simple functin in Javascript to call to ensure that the email address is valid(in that it atleast has an @ sign, and may be a . in it)  This is simple parsing, but I don't know javascript and have no resourses here with me.
LVL 1
khagenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xabiCommented:
Here goes the function I use:

function is_email(address) {
      var reEmail = /^(.+\@.+\..+)*$/
      if (reEmail.test(address)) {
            alert("valid")
      } else {
            alert("invalid")
      }
}

is javascript 1.2 so only works in latest browsers.

If you want a more compatible funcion I can write it for you.

xabi
0
khagenAuthor Commented:
Thanks thats great.  But I would like, instead of a alert, a variable be set to True, so that I could easily fit it in with this group;

IF Request("fname") = "" OR _
      Request("lname") = "" OR _
      Request("address1") = "" OR _      
      Request("city") = "" OR _
      Request("state") = "" OR _
      Request("zip") = "" OR _
      Request("phone") = "" OR _
      is_email(Request("email")) = True Then
      'Request("email") = "" Then
            msg = "<font color='#003399'><TT><B><I>One or more of the required fields was not filled out.</I></B></TT></font>"
      END IF
0
mblaseCommented:
According to the Perl Cookbook by O'Reilly, "There isn't one. You cannot do real-time validation of mail addresses. You must pick from a number of compromises. The common patters that people try to use for this are all quite incorrect."

However, you can at least validate according to your specifications this way, for all JavaScript-enabled browsers:

function verifyEmail(addr) {
    var atPos = addr.indexOf('@');
    var dotPos = addr.indexOf('.')
    if (atPos>=0 && dotPos>atPos) {
        return true;
    } else {
        alert("You entered an invalid email address.");
        return false;
    }
}


Then you can put something like this in your FORM tag:

<FORM onSubmit="return verifyEmail(document.forms[0].email.value)">
    <input name="email">
    <input type="submit">
</FORM>
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

khagenAuthor Commented:
Thanks, I have faith that this will work, Java script seems pretty convenient for this.  However, I am getting a Typ mismatch error on VerifyEmail.  How do you convert to string.?  Right now I have

verifyEmail(Request("email"))= False Then

0
xabiCommented:
function is_email(address) {
  var reEmail = /^(.+\@.+\..+)*$/
  if (reEmail.test(address)) {
    return true
  } else {
    return false
  }
}

that's all

if you want you can use mblase script too.

xabi
0
khagenAuthor Commented:
I am still getting a type mismatch error.  I don't understand why.  Here is a large section of what I have now;


function is_email(address) {
  var reEmail = /^(.+\@.+\..+)*$/
  if (reEmail.test(address)) {
    return true
  } else {
    return false
  }
}


</script>

<%
IF Request("fname") = "" OR _
      Request("lname") = "" OR _
      Request("address1") = "" OR _      
      Request("city") = "" OR _
      Request("state") = "" OR _
      Request("zip") = "" OR _
      Request("phone") = "" OR _
      is_email(Request("email"))= False Then
      'Request("email") = "" Then
            msg = "<font color='#003399'><TT><B><I>One or more of the required fields was not filled out.</I></B></TT></font>"
      END IF
%>
0
mblaseCommented:
I think you want

verifyEmail(Request("email"))==False

or better yet:

!verifyEmail(Request("email"))

I suggested my code rather than xabi's only because it's compatible with all versions of JavaScript, not just 1.2. (Sorry, xabi. :) )

0
khagenAuthor Commented:
I am now getting a syntax error on ! and ==, no matter which I use.
0
khagenAuthor Commented:
If I put;
dim email
email = Request("email")
IF Request("fname") = "" OR _
      Request("lname") = "" OR _
      Request("address1") = "" OR _      
      Request("city") = "" OR _
      Request("state") = "" OR _
      Request("zip") = "" OR _
      Request("phone") = "" OR _
      verifyEmail(email) = True Then

I get a type mismatch. Are there problems between VBscript and Java Script?
0
mblaseCommented:
Ahh, I see. Try this then:

....
Request("phone") = "" OR _
verifyEmail(email) Then
....
0
mblaseCommented:
..I've never tried mixing JScript and VBScript in the same ASP code, which is what you suddenly appear to be trying to do. The function I gave you was intended to provide verification *before* the form was submitted.

Here's a VBScript function you can use that should do the same verification in ASP:


Function verifyEmail(strAddr)
  intAtPos = InStr(strAddr,"@")
  intDotPos = InStrRev(strAddr,".")
  verifyEmail = (intAtPos>0 AND intDotPos>intAsPos)
End Function

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mblaseCommented:
And while I'm being compulsive, here's a more accurate and concise version of my JavaScript function. :)

function verifyEmail(addr) {
    var atPos = addr.indexOf('@');
    var dotPos = addr.lastIndexOf('.')
    if (atPos>=0 && dotPos>atPos) return true;
    else alert("You entered an invalid email address.");
    return false;
}
0
khagenAuthor Commented:
I ended up using mblast's vbScript.
0
khagenAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.