Live data from Hacker News

HTML Form Validation is underused

expressionstatement.com

1–10 of 343 posts

Re: HTML Form Validation is underused

#2
simply adding required is all you need.Not required=true The omission is equal to required=false. No one really write required=true, they just add the attribute `required` only by its self. This is one of the odd quarks about html attrs

Same is true for things like disabled ect https://developer.mozilla.org/en-US/docs/Glossary/Boolean/HT...

> The strings "true" and "false" are invalid values. To set the attribute to false, the attribute should be omitted altogether. Though modern browsers treat any string value as true, you should not rely on that behavior.

in other words required=false may still end up making the field required. FYI.

Re: HTML Form Validation is underused

#3
You gotta be careful about going overboard with it.

Recently I was trying to get a refund on Groupon because the company I'd bought a Groupon for was under new management that refused to honor my groupon.

The form had a stipulation "minimum of 15 words". Try as I might, I could not get the form to pass validate until I inspected the HTML.

  

  \w - word characters
  \b - word boundaries
  \s - white
Literally zero allowance for any sort of punctuation.

Re: HTML Form Validation is underused

#5
post #3

You gotta be careful about going overboard with it. Recently I was trying to get a refund on Groupon because the company I'd bought a Groupon for was under new management that refused to honor my groupon. The form had a stipulation "minimum of 15 words". Try as I might, I could not get the form to pass validate until I inspected the HTML. \w - word characters \b - word boundaries \s - white Literally zero allowance f…

I don't think this is particularly pertinent to HTML validation. This is true of any type of validation. The same rule could have been applied on the server, and then you would have had no hope.

Re: HTML Form Validation is underused

#6
post #4

That's nice, I'll use that next time. Although it always feels kind of bad to write client side validation code because you're going to have to do the same checks on the server side anyway.

Do you make your users do a server round-trip to see what's wrong? To have good UX, you kind of have to do both.

Re: HTML Form Validation is underused

#7

simply adding required is all you need.Not required=true The omission is equal to required=false. No one really write required=true, they just add the attribute `required` only by its self. This is one of the odd quarks about html attrs Same is true for things like disabled ect https://developer.mozilla.org/en-US/docs/Glossary/Boolean/HT... > The strings "true" and "false" are invalid values. To set the attribute to…

They've written it in JSX, I think, not in HTML.

Re: HTML Form Validation is underused

#8

simply adding required is all you need.Not required=true The omission is equal to required=false. No one really write required=true, they just add the attribute `required` only by its self. This is one of the odd quarks about html attrs Same is true for things like disabled ect https://developer.mozilla.org/en-US/docs/Glossary/Boolean/HT... > The strings "true" and "false" are invalid values. To set the attribute to…

They've used `required={true}`, not `required="true"`, which is JSX, not HTML. The one with curlies isn't even valid HTML. In the old HTML spec, the correct value, if you wanted to set a value, was to set `required="required"`, but these days the spec is looser since it tries to conform to the web, not the other way around.

Re: HTML Form Validation is underused

#9
One of the things I dislike about HTML form validation is it starts running from page load. So if e.g. you tie error state formatting to it, the form loads up with a bunch of errors which may be intimidating to the user.

Re: HTML Form Validation is underused

#10
post #4

That's nice, I'll use that next time. Although it always feels kind of bad to write client side validation code because you're going to have to do the same checks on the server side anyway.

I find it sad that so many frameworks leave the developer to duplicate server-side and client-side validation. There are obviously some things you can't reasonably validate on the client, but I'd like to see more automated ways to take backend constraints and check them on the client too.

Ideally constraints would also propagate from model definitions, so there could be a single source of truth for "what phone numbers do we accept". Some years back I tried to do this by parsing SQL table definitions, but never got far enough. Django does this, but it lacks pretty much any client-side validation support IIRC. (Or client-side anything, really...)

Post reply on HN