Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java script email validation

Posted on 2008-06-19
6
Medium Priority
?
193 Views
Last Modified: 2013-11-23
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
Comment
Question by:robrodp
6 Comments
 
LVL 17

Expert Comment

by:contactkarthi
ID: 21825884
if (document.forma.email.value="" || !validateEmail(document.forma.email.value))
0
 
LVL 17

Assisted Solution

by:contactkarthi
contactkarthi earned 400 total points
ID: 21825893
oops

if (document.forma.email.value==null || !validateEmail(document.forma.email.value))
0
 

Author Comment

by:robrodp
ID: 21826006
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:robrodp
ID: 21826552
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
 
LVL 13

Assisted Solution

by:MicheleMarcon
MicheleMarcon earned 200 total points
ID: 21828730
return (fieldValue.length>0);
0
 
LVL 3

Accepted Solution

by:
sreehariharan earned 1400 total points
ID: 21946340
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question