All Projects β†’ insin β†’ Get Form Data

insin / Get Form Data

Licence: mit
Gets form and field data via form.elements

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Get Form Data

Autosize Input
🎈 Effortless, dynamic-width text boxes in vanilla JavaScript
Stars: ✭ 64 (-34.02%)
Mutual labels:  forms
Wtforms
A flexible forms validation and rendering library for Python.
Stars: ✭ 1,214 (+1151.55%)
Mutual labels:  forms
Formio
A Form and Data Management Platform for Progressive Web Applications.
Stars: ✭ 1,245 (+1183.51%)
Mutual labels:  forms
Ng Dynamic Forms
Rapid form development library for Angular
Stars: ✭ 1,146 (+1081.44%)
Mutual labels:  forms
Google Forms Html Exporter
Build a standard HTML <form> using any Google Forms
Stars: ✭ 74 (-23.71%)
Mutual labels:  forms
Formium
The headless form builder for the modern web.
Stars: ✭ 78 (-19.59%)
Mutual labels:  forms
React Router Form
<Form> is to <form> as <Link> is to <a>
Stars: ✭ 58 (-40.21%)
Mutual labels:  forms
Formatic
✏️ A configurable, pluggable forms library for React used on Zapier.com.
Stars: ✭ 93 (-4.12%)
Mutual labels:  forms
Laravel Multistep Forms
Responsable Multistep Form Builder for Laravel
Stars: ✭ 76 (-21.65%)
Mutual labels:  forms
React Native Merlin
πŸ§™ Simple web-like forms in react native.
Stars: ✭ 83 (-14.43%)
Mutual labels:  forms
Formst
Model-driven Form library for React
Stars: ✭ 69 (-28.87%)
Mutual labels:  forms
Bind Query Params
Sync URL Query Params with Angular Form Controls
Stars: ✭ 73 (-24.74%)
Mutual labels:  forms
React Inform
Simple controlled forms with validations in react
Stars: ✭ 81 (-16.49%)
Mutual labels:  forms
Mform
Spielend einfach umfangreiche Modul-Input-Formulare erzeugen.
Stars: ✭ 65 (-32.99%)
Mutual labels:  forms
Flask Wtform Tutorial
πŸ“πŸ˜ŽTutorial to implement forms in your Flask app.
Stars: ✭ 84 (-13.4%)
Mutual labels:  forms
Customui
Library to create custom UI's in MCPE 1.2+
Stars: ✭ 60 (-38.14%)
Mutual labels:  forms
Normform
Normform: A tiny CSS plugin to make your web forms beautiful again
Stars: ✭ 78 (-19.59%)
Mutual labels:  forms
Swiftforms
A small and lightweight library written in Swift that allows you to easily create forms.
Stars: ✭ 1,329 (+1270.1%)
Mutual labels:  forms
Rsformview
A Cocoapods library designed to easily create forms with multiple data entry fields
Stars: ✭ 84 (-13.4%)
Mutual labels:  forms
Select Css
Cross-browser styles for consistent select element styling
Stars: ✭ 1,242 (+1180.41%)
Mutual labels:  forms

get-form-data

Travis npm package Coveralls

Gets form data - or data for a named form field - via form.elements.

Data is retrieved in a format similar to request parameters which would be sent if the form was submitted, so this module is suitable for extracting form data on the client side for projects which implement isomorphic handling of form submission.

Install

npm install get-form-data

Browser bundles area available, which export a global getFormData function.

Usage

Getting form data

To get data for an entire form, use the getFormData() function:

<form id="productForm">
  ...
  <label>Product:</label>
  <select name="product">
    <option value="1" selected>T-shirt</option>
    <option value="2">Hat</option>
    <option value="3">Shoes</option>
  </select>

  <label>Quantity:</label>
  <input type="number" name="quantity" min="0" step="1" value="9">

  <label>Express shipping</label>
  <p>Do you want to use <a href="/shipping#express">Express Shipping</a>?</p>
  <div class="radios">
    <label><input type="radio" name="shipping" value="express" checked> Yes</label>
    <label><input type="radio" name="shipping" value="regular"> No</label>
  </div>

  <label>Terms of Service:</label>
  <p>I have read and agree to the <a href="/">Terms of Service</a>.</p>
  <label class="checkbox"><input type="checkbox" name="tos" checked> Yes</label>
  ...
</form>
let form = document.querySelector('#productForm')

let data = getFormData(form)

console.log(JSON.stringify(data))
{"product": "1", "quantity": "9", "shipping": "express", "tos": true}

Getting field data

To get data for individual form fields (which may contain multiple form inputs with the same name), use the getFieldData() function, which is exposed as a property of getFormData:

<form id="tshirtForm">
  ...
  <label>Sizes:</label>
  <div class="checkboxes">
    <label><input type="checkbox" name="sizes" value="S"> S</label>
    <label><input type="checkbox" name="sizes" value="M" checked> M</label>
    <label><input type="checkbox" name="sizes" value="L" checked> L</label>
  </div>
  ...
</form>
let form = document.querySelector('#tshirtForm')

let sizes = getFormData.getFieldData(form, 'sizes')

console.log(JSON.stringify(sizes))
["M", "L"]

Trimming user input

To trim user input, pass a trim: true option to getFormData() or getFieldData():

<form id="signupForm">
  ...
  <label>Username:</label>
  <input type="text" name="username" value="AzureDiamond  ">

  <label>Password:</label>
  <input type="password" name="password" value=" hunter2 ">
  ...
</form>
let form = document.querySelector('#signupForm')

let data = getFormData(form, {trim: true})

console.log(JSON.stringify(data))
{"username": "AzureDiamond", "password": "hunter2"}

Including disabled inputs

Disabled inputs are ignored by default; to include their data, pass an includeDisabled: true option to getFormData() or getFieldData().

let data = getFormData(form, {includeDisabled: true})

File Inputs

Where possible, data extracted from <input type="file"> will be native File objects.

If the .files property is not available, the .value property will be used to provide data instead.

API

getFormData(form: HTMLFormElement[, options: Object])

Extracts data from a <form>'s .elements collection - in order to use .elements, form inputs must have name or id attributes. Since multiple inputs can't have the same id and a name allows an input to qualify as a successful control for form submission, name attributes are preferred and will be given priority if both are present.

The following options can be configured:

  • trim: Boolean (default: false) - if true, leading and trailing whitespace will be trimmed from user input in text entry form inputs.
  • includeDisabled: Boolean (default: false) - if true, disabled inputs will not be ignored.

Return type: Object<string, boolean | string | string[] | File | File[]>

Properties in the returned data object are mostly consistent with what would have been sent as request parameters if the form had been submitted:

  • Disabled inputs are ignored by default.
  • Text inputs will always contribute a value, which will be '' if they are empty.
  • Checkbox inputs will only contribute a value if they are checked, in which case their value attribute will be used.
  • Form elements which represent multiple values (select-multiple, or multiple inputs with the same name, file inputs with multiple) will only contribute a value if they have at least one value to submit. Their values will always be held in an Array, even if there is only one.

Exceptions to this are:

  • If a checked checkbox doesn't have a value attribute, its value will be true. Normally it would default to 'on' when submitted, but this isn't as useful a default on the client.
  • Buttons are completely ignored, as it's only possible to determine which button counts as successful after it's been used to submit the form.

getFieldData(form: HTMLFormElement, fieldName: String[, options: Object])

getFieldData() is a named export when using ES modules, otherwise it's also available as getFormData.getFieldData()

Extracts data for a named field from a <form>'s .elements collection.

Options are as documented for getFormData.

Return type: null | boolean | string | string[] | File | File[]

This function is used by getFormData(), so the documentation for individual return values above also applies.

null will be returned if the field is non-existent, disabled, or shouldn't contribute a value (e.g. unchecked checkboxes, multiple selects with no selections, file inputs with no selections).

MIT Licensed

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].