Solved

java script email validation

Posted on 2008-06-19
6
187 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 100 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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 50 total points
ID: 21828730
return (fieldValue.length>0);
0
 
LVL 3

Accepted Solution

by:
sreehariharan earned 350 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 64
custom annotations 9 42
servlet example 11 49
use lov values 2 61
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

792 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