MrShabana
MrShabana

Reputation: 377

What type of form input to be expected from a user that would yield null in javascript?

I'm not sure if my question is clear enough, but consider the following function:

function validateForm() {
    var x = document.forms["myForm"]["fname"].value;

    if (x == null || x == "") {
        alert("First name must be filled out");
        return false;
    }
}

What I don't get is "null"; I understand that an empty string "" indicates nothing has been inputted, but what would be the type of input by a user that would imply null?

Upvotes: 3

Views: 81

Answers (1)

David G
David G

Reputation: 96810

The value attribute of an input element returns a string, which will contain the text inputted by the user, or otherwise it will be empty (""). There is not and should not be a reason to check for null in the condition because a string (empty or not) can never be equal to null.

Upvotes: 3

Related Questions