I sea a  javascript funtion to validate emails... I need it to pass when the email field is empty

Posted on 2008-06-19
Last Modified: 2013-11-23
The function always returns false when the email is empty... I guess a change 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!!!
Question by:robrodp
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Accepted Solution

krishna kishore mellacheruvu venkata earned 250 total points
ID: 21828638
Try this code

 function validateEmail(fieldValue) {
      if (fieldValue!=null && fieldValue.length > 0)
     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);
       return true;

Assisted Solution

manuel_m earned 250 total points
ID: 21828893
You can check for null and for undefined. It's only an extension of the code from melchkishore :-)

function validateEmail(fieldValue) {
      if ((fieldValue != null || typeof(fieldValue) == "undefined") && fieldValue.length > 0)
     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);
       return true;

Open in new window


Expert Comment

ID: 21903905
Notice that you post this in the "new to java" zone. This is a JavaScript question and should really be posted in the zone with the same name. JavaScript != Java.
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.


Author Comment

ID: 21903929
Mh... I am relly new right?

LVL 10
ID: 21905492
Did your problem got solved?

Author Comment

ID: 21907180
Yes... I am sending the Accept as solution right now

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tomcat: Unable to run tomcat service. 2 34
Which non-HTML GUI front end to use with Java? 3 67
Html Table Looping (part 2) 5 41
Java ArrayList and if statement 2 55
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

739 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