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?
 
mblaseConnect With a Mentor Commented:
..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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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:
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
All Courses

From novice to tech pro — start learning today.