I’m trying to get this bit of code in my cfscript tag to work. I’ve searched and searched but have some up with nothing which will work to validate the email address.
If the email address is blank it will return the errormessage but if I just type on character, it will let it pass.
I’m pretty new to scripting in CF so any help would be appreciated.
if (isDefined("form.email"))
{
if (form.email is "")
{
errormessageemail = "Please enter a valid Email Address!";
}
else if (not form.email is "")
{
email = form.email;
function validate_email(str,email) {
if( not len(trim(arguments.str)) or not refind("^[0-9A-Za-z.'+_-]+@([0-9A-Za-z-]+\.)+[A-Za-z]+$", trim(arguments.str)) ) {
errormessageemail = "Please enter a valid Email Address! Ex. abc@abc.com";
}
return errormessageemail;
}
}
}
It could be as simple as this:
You don’t validate if email is not posted. Blank string is not valid email, no need to check it specially.
Note: someone may argue that isValid/email does not work 100% properly. That’s true, but rare problem. Use regex if you think so as well.