• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

Email Validation in a CFFORM

Can someone post a script for validatiing an email address in a CFFORM? Currently I have it required='yes' but I have no validation for the format. A user could enter joeblow@@domain...com and the form would be processed as normal. Does anyone have any code I could use?

Thanks,

IShiva
0
IShiva
Asked:
IShiva
  • 3
  • 2
1 Solution
 
kyle1830Commented:
here is a js function that I use.
<script>
    function checkEmail(strEmail) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strEmail))
        {
            return (true);
        } else {
            return (false);
        }
    };
</script>

kyle
0
 
IShivaAuthor Commented:
Can you post some code to help me in placing that code on the page? Im not sure where is goes or how to get it to work. Thanks kyle

IShiva
0
 
kyle1830Commented:
here is an example before the submit button is clicked:
<script>
    function checkEmail(strEmail) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strEmail))
        {
            return (true);
        } else {
                    alert ('Fix the email!!');
        }
    };
</script>
<form action="index.cfm" method="post">
      <input type="text" name="test" onblur="checkEmail(this.value);">
      <br>
      <input type="Submit" name="submit" value="submit">
</form>


then the same thing but when the button is clicked
<script>
    function checkEmail() {
       var a = document.testform.test.value
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(a))
        {
            return (true);
        } else {
                    alert ('Fix the email!!');
         return (false);
        }
    };
</script>
<form action="index.cfm" method="post" name="testform" onSubmit="return checkEmail();">
      <input type="text" name="test" value="">
      <br>
      <input type="Submit" name="submit" value="submit">
</form>

hope this helps
kyle


0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
tnunamakCommented:
Use this UDF from www.cflib.org

<cfscript>
/**
 * Tests passed value to see if it is a valid e-mail address (supports subdomain nesting and new top-level domains).
 * Update by David Kearns to support '
 * SBrown@xacting.com pointing out regex still wasn't accepting ' correctly.
 *
 * @param str        The string to check. (Required)
 * @return Returns a boolean.
 * @author Jeff Guillaume (jeff@kazoomis.com)
 * @version 2, August 15, 2002
 */
function IsEmail(str) {
        //supports new top level tlds
if (REFindNoCase("^['_a-z0-9-]+(\.['_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(([a-z]{2,3})|(aero|coop|info|museum|name))$",str)) return TRUE;
      else return FALSE;
}
</cfscript>

0
 
IShivaAuthor Commented:
Thanks Kyle, worked like a charm!

sorry tnunamak, i didnt try yours since the previous post worked...but thanks for posting!!

ishiva
0
 
kyle1830Commented:
you're welcome ishiva......good luck
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now