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

java script email validation

I have a funcion that validates email quite nicely.. the lines:
              if (!validateEmail(document.forma.email.value)) {
           alert("La dirección de correo electrónico no es valida.");
           return false;
               }      
Do the actual validation.
What do I have to add at this level to have it return true if  the field is empty. That is either it is a vaid email or empty..
0
robrodp
Asked:
robrodp
3 Solutions
 
contactkarthiCommented:
if (document.forma.email.value="" || !validateEmail(document.forma.email.value))
0
 
contactkarthiCommented:
oops

if (document.forma.email.value==null || !validateEmail(document.forma.email.value))
0
 
robrodpAuthor Commented:
It is returning an error when the field is empty I need an ok validation when the field is empty or whatever is in the field passes the validemail test. I am not sure if null and "" are the same
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
robrodpAuthor Commented:
Mh.... I guess the function always returns false when the email is empty... I guess the cjage has to be done at the validation function level. The function is:

function validateEmail(fieldValue) {
     return (/^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/).test(fieldValue);
}

It returns false if the emailis empty... cant figure out why!!!
0
 
MicheleMarconCommented:
return (fieldValue.length>0);
0
 
sreehariharanCommented:
Hi
Just Change the method as

function validateEmail(fieldValue) {
if(fieldValue !=null)
    return true;
 else
return (/^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/).test(fieldValue);
}

or u can validate away from the function also

 if (!validateEmail(document.forma.email.value)) {
if(document.forma.email.value)
{ return true;}
else
{          alert("La dirección de correo electrónico no es valida.");
           return false;
               }      

}

Just check it and let me know if there is any further issues
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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