Validating an Email address Form Field (Full Version)

All Forums >> [Web Development] >> Microsoft FrontPage Help



Message


Peppergal -> Validating an Email address Form Field (3/21/2008 11:59:37)

I've had issues with people not knowing to put an email address in the form field - even though it clearly says "Email Address."

If someone fills in the form without their email address, or if they don't put the @whatever.com (believe it or not, some people have just put their username without the rest of the address) there is no way for me to contact them.

This form is voluntary, if they want to be contacted by us....it's not required, so it's important that they tell us how to contact them.

I tried validating the email address form field so that the form will only go through if the user puts in an actual email address.

If i put the @ sign under "other" , then if there is a dash, underscore, or period in the email address, the form won't go through, as it says only the @ , letters, and digits are allowed....a period or underscore or dash will make it not allowed.

If I put @ . - _ in the "other" field, it seems to let ANYTHING go through, even if there are none of those characters.

I'm at a loss on how to validate this so that only something with a proper email address format goes through.

The form I'm trying to validate is this one:

http://www.paupackgroup.com/sell.htm

I even had one email address come through as "Hawley, PA"

It is surprising but true. LOL




coreybryant -> RE: Validating an Email address Form Field (3/21/2008 15:35:12)

I think this might be the script - so many of them but it might make sure there is a proper email format
<script language="JAVASCRIPT">
<!--
function isEMail( strValue) {
  var objRegExp  = /^[a-z]\w*([.\-]\w+)*@[a-z]\w*([.\-]\w+)*\.[a-z]{2,3}$/i;
  return objRegExp.test(strValue);
}
function validate(theForm){
    if(theForm.EMail.value == ""){
         alert('Please enter a valid E-mail address to submit the form');
         theForm.EMail.focus();
         return false;
    }
            if(!hasValue(theForm.Name.value)){
        alert("Please enter your Name.");
               theForm.Name.focus();
             return false;
}
    if(!isEMail(theForm.EMail.value)){
         alert('Please enter a valid E-mail address to submit the form');
         theForm.EMail.select();
         theForm.EMail.focus();
         return false;
         }
    return true;
}
// -->
</script>




rdouglass -> RE: Validating an Email address Form Field (3/22/2008 1:06:12)

An alternative that I use often is ASP once the form is posted. Preference is usually to validate at the client but sometimes you need to validate at the server as well. I use an ASP function like this:

<%Function RegExpTest(sEmail)
RegExpTest = false
Dim regEx, retVal
Set regEx = New RegExp

' Create regular expression for emails
regEx.Pattern ="^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$"

regEx.IgnoreCase = true
retVal = regEx.Test(sEmail)
If not retVal Then
exit function
End If

RegExpTest = true
End Function%>

and use it like so:

<%If RegExpTest(Request.form("EmailFormField")) Then
'Do something when valid email
Else
'Do something else
End If%>

Just an alternative. Personally I tend to validate at the server as well since we can't always rely on the JS and it doesn'ty slow things down much at all.




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
6.054688E-02