0

I'm working through a tutorial right now on form validation, and I would like assistance on why this function always returns the if condition.

<html>
<head>
</head>
<body>
     <form id="form" action="#" method="post">
        <fieldset>
            <p>
                First Name:
                <input type="text" id="txt" />
            </p>
            <p> <input type="submit" value="submit" onclick="validate()" /> </p>
        </fieldset>
    </form>
    <script type="text/javascript"> 
        function validate() 
            {
            var userName = document.getElementById("txt").value;
            if (userName.length == 0)
                {
                alert("FINISH THAT UP");
                return false;
                } 
            else 
                {
                alert("thanks, " + UserName);
                }
            }
    </script>
</body>

3
  • Is the value not always empty? Commented Jul 1, 2012 at 7:37
  • javascript.crockford.com/code.html Commented Jul 1, 2012 at 7:39
  • It shouldn't be if the user enters something for the input. Commented Jul 1, 2012 at 7:40

4 Answers 4

4

It does reach the else part when the textbox is not empty, but then it crash with error thus the form is being silently submitted.

JavaScript is case sensitive - UserName is different than userName thus undefined.

Change to:

alert("thanks, " + userName);

To avoid such problems, you can wrap the whole block with try..catch like this:

function validate() 
{
    try
    {
        var userName = document.getElementById("txt").value;
        if (userName.length == 0)
        {
            alert("FINISH THAT UP");
            return false;
        } 
        else 
        {
            alert("thanks, " + UserName);
        }
    } catch (err) {
        alert("general error while validating: " + err.message);
        return false;
    }
}

With this in place, you would see "UserName is undefined".

Live test case.

Also, on a side note - with your current code, even with empty value the form will still be submitted although you return false - that's because you need to cancel the submit even:

<input type="submit" value="submit" onclick="return validate()" />

You forgot the return keyword in the onclick itself.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow that was really thorough. Thanks a million.
No problem, there is even more but I'll just mention this as advice - bind the event in a global onload instead of inline. This one doesn't really matter, just more elegant way to achieve the same thing. :)
3

JavaScript is case-sensitive language. So correct the userName variable name in else statement as below.

  else {
      alert("thanks, " + userName); 
  }

2 Comments

Thanks a lot! I am puzzled though as to why that affects the alert, shouldn't at least "thanks, " show up?
@T-Bone - trying to access an undefined variable is a run-time error in javascript, throwing an exception and stopping execution of the script.
0

Can you try this? I think this could work.

function validate() 
{
    try
    {
        var userName = document.getElementById("txt").value;
        if (userName)
        {
            alert("FINISH THAT UP");
            return false;
        } 
        else 
        {
            alert("thanks, " + UserName);
        }
    } catch (err) {
        alert("general error while validating: " + err.description);
    }
}

2 Comments

Is it just pure chance you used the exact variable name I used and done same mistake like I did and fixed since?
Did this fix it? I just used the exact variable but i hadn't tried the code itself since logically it works but i dunno if it really does.
0

what about this

function validate(){
    $("#form").submit(function() {

    if($("#txt").val()==""){
        alert("FINISH THAT UP");
        return false;
    }
    if($("#txt").val()!=""){
        var un = $("#txt").val();
        alert("thanks, " + un);
        return true;
    }
   });

}

Comments

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.