Skip to main content

forms

Helpers for serializing, resetting, and populating HTML forms, plus native validation and submit handling.

Import: @rtorcato/browser-common/forms

๐Ÿ“– MDN: HTMLFormElement

Browser supportโ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
FormDataโœ… 5+โœ… 12+โœ… 4+ [1]โœ… 5+โœ… 5+Widely available
HTMLFormElement.reportValidityโœ… 40+โœ… 17+โœ… 49+โœ… 10.1+โœ… 10.3+Widely available
  1. Firefox: Before Firefox 7, specifying a Blob as the data to append to the object, the filename reported in the Content-Disposition HTTP header was an empty string, resulting in errors on some servers. Starting with Firefox 7, the filename blob is sent.

โœ… supported ยท โ— partial ยท โŒ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: FormData ยท HTMLFormElement.reportValidity

Exampleโ€‹

import { serializeForm, isFormValid, onFormSubmit } from '@rtorcato/browser-common/forms'

const off = onFormSubmit(myForm, (event) => {
if (!isFormValid(myForm)) return
const data = serializeForm(myForm)
console.log(data)
})

off()

Exportsโ€‹

  • serializeForm(form) โ€” returns form field values as Record<string, string>
  • resetForm(form) โ€” resets the form to its initial values
  • setFormValues(form, values) โ€” writes values onto matching named fields
  • isFormValid(form) โ€” runs the browser's built-in checkValidity()
  • onFormSubmit(form, callback) โ€” listens for submit, calls preventDefault() first; returns an unsubscribe function

See the API reference for full signatures.