Skip to content

Instantly share code, notes, and snippets.

@zachleat
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save zachleat/9643862 to your computer and use it in GitHub Desktop.

Select an option

Save zachleat/9643862 to your computer and use it in GitHub Desktop.
Requirements/Notes on JavaScript Form Validation

Two classes of validators:

  • Required (empty/non-empty) validation
  • Format validation (phone number, zip/postal code, etc)

Don’t show both a required error and a format error at the same time. In other words, format validation errors are NOT shown for empty fields (doesn’t matter if they are required or not).

Pseudocode for an isValid method:

if( valueNotEmpty ) {
  return hasFormatValidator ? isFormatValidatorValid() : true;
} else {
  return !isRequiredField;
}

Side note: A more complicated version of this has two different modes of operation for presubmit and postsubmit (tried to submit, but fields have errors), but may not produce enough value for the code required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment