1

I wrote a javascript function to validate that the user input data is not empty. The function called "isNotEmpty()" identifies that the text box is empty. But however the main function returns a true statement to the JSP page. Thus the form gets submitted.

The alert no 2&3 fires according the input from the form. but the Alert No1 dose not fires in any circumstances.

Please check on this and assist me. Thank you.

var firstName;
var data  = new Array(); 

function formEntryValidationTest() {
    firstName = document.forms["regForm"]["firstName"];
    data[0] = isNotEmpty(firstName);//check the first name is empty

    for(var i=0;i<data.length;i++) {
        if(data[i]=="false")
        {
            alert("Check"); //alert No-1
            return false;
        }
    }
}

// this is the function to check whether the input is not empty
function isNotEmpty(obj) {
    if(obj.value!="") {
        alert("true"); //alert No-2
        return "true";
    }
    else
    {
        alert("false");//alert No-3
        return "false";
    }
}
1

2 Answers 2

4

You're returning the string "false" rather than the boolean value false, which in JavaScript evaluates to a "truthy" value.

Change the return statements in isNotEmpty to return true; and return false; and the condition in formEntryValidationTest to if (!data[i]).

Also, the condition you check in isNotEmpty is too strict. It will return true for undefined and null, which may be the reason your code isn't working. Change it to something like this:

function isNotEmpty(value){
  return value ? value.length > 0 : false;
}

Then pass the input value directly rather than passing the input object itself:

data[0] = isNotEmpty(firstName.value);
Sign up to request clarification or add additional context in comments.

4 Comments

No generally in the form onSubmit, I calls the formEntryValidationTest() function.
I changed the source code according to your instructions, no improvement.
I found a clue. When I commented the "data[0] = isNotEmpty(firstName.value)" and changed the condition as "if(!isNotEmpty(firstName.value))", it worked perfectly. Meanwhile I taken-off the comment which I mentioned here same problem occurred. Can you say why is it?
Here are my files. You'll find error even I made the changes according your instructions. I don't know why. Please refer the link mediafire.com/?9kac6352z6749z0
1

You are using strings "true" and "false" instead of boolean values true and false. Remove the quotes as in:

   return true

instead of 

   return "true"

Do the same in your if statement:

    if(data[i]==false)
instead of
    if(data[i]=="false")

(Note that there are no quotes)

1 Comment

I did but the issue remains same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.