10

I'm pretty sure it's not possible to do this but I'm still asking for it.

Is there a way for a user to insert a null (or an undefined) value into an html text input?

I need to distinguish between a null value and an empty string and I would like to allow the user to set this value into a single input.

For what I can see across the web, the standard solution to this problem is to match the text input with a checkbox that tell if the value is null or not. From my understanding, the limitation is linked to the fact that the textbox can hold an empty string but not a null value so that's why I think it's not possible to do exactly what I want.

The_Black_Smurf
  • 5,178
  • 14
  • 52
  • 78
  • The limitation is not what it can or cannot hold - it is about what the browser sends to the server. When you have a ``, `example=` will be sent to the server - there is no distinguishing "this is null" vs "this is an empty string". With a checkbox, if it is checked, the value is sent to the server, if it isn't it isn't. – Oded Jun 21 '13 at 16:53
  • Each value I send to the server are pre-handled by javascript so it could be send with the right value. The lack of Null support in the browser's text input is really the root of my issue. – The_Black_Smurf Jun 21 '13 at 17:50

2 Answers2

9

You can't tell a text input to have any kind of null value. Empty checkboxes have a value of an empty string '' by definition. The best way to do it, as you say, is to have a checkbox that toggles the disabled property of the text input, which gives a similar semantic.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318
2

Necromancing.
The accepted answer is kindof wrong - there is a way, sort of.
If you set the textbox state to disabled (for example with the chrome developer tools), then it won't submit the input box's value.
Hence, if you deserialize the form model from a post-request, the text's value will be NULL, unless your deserializer deliberately changes data.

Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442