This site uses cookies. By continuing, your consent is assumed. Learn more

139.3fm shares

Validating text fields in javascript


HTML5 introduces a couple of new attributes for implementing browser-based form validation. The pattern attribute is a regular-expression that defines the range of valid inputs for textarea elements and most types of input.

The required attribute specifies whether a field is required. We can also use them to provide a more interesting Validating text fields in javascript — instant form validation. This is very poor usability, and directly contravenes accessibility guidelines. If it did, then every required field would have that outline by default. As an alternative, we could use the oninput event, which fires as soon as any value is typed Validating text fields in javascript pasted into the field.

But this is really too instant, as it could easily be triggered on and off many types in rapid succession while typing, creating a flashing effect which would be annoying or impossibly distracting for some users.

This example is a simple comments form, in which some fields are required, some are validated, and some are both.

News feed