0

I want to validate an HTML form to ensure an <input> has a value. In JavaScript, what is the value of an empty field in a form: null or the empty string ("")?

5
  • 5
    did you try to see for yourself what it was??? Commented Apr 29, 2011 at 18:21
  • Seems like it would be fairly simple to test both of these methods. Did you try it already and are getting ambiguous results? Commented Apr 29, 2011 at 18:22
  • SO shouldn't be your first choice. Try yourself and if you think you are unable to find out and you really need a help, try SO. Help keep SO clean. Commented Apr 29, 2011 at 18:25
  • You could always alert or log document.getElementById(idname).value to see the value. Commented Apr 29, 2011 at 18:25
  • 1
    No I didn't try it myself. There are a lot of examples online that give different answers, so I wanted to ask the experts, in case there are cross-browser compatibility issues or other snafus. Commented Apr 29, 2011 at 18:30

3 Answers 3

3

For any input element, value is always a string. This means you can expect it to be "", when empty.

See the docs for HTMLInputElement on the Mozilla Developer Network, or the relevant part of the HTML4 spec.

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

Comments

0

Empty string, if it's a text field.

Comments

0

It's an empty string -- one of the beautiful perks of JavaScript.

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.