@reacthooks.org/use-form-validation

1.0.9 • Public • Published

Install

$ npm install @reacthooks.org/use-form-validation

useFormValidation

React Hook to help with forms.

Usage

import useFormValidation from 'use-form-validation';

const initialFormState = {
  email: '',
}
function validate(values) {
  console.log('validate()')
  // check email address
  return { email: 'Invalid email address' }
}

function FormValidation() {
  const { values, errors, onChange, onBlur, onSubmit } = useFormValidation(initialFormState, validate, values => {
    // called when there has been a change and everything validaates
    console.log('values:', values)
  })

  return (
    <form onSubmit={ onSubmit }>
      <label>
        <span>Email</span>
        <input name="email" value={ values.email } onBlur={ onBlur } onChange={ onChange } />
      </label>
      <br />
      {
        errors.email && <p style={ { color: 'red' } }>{ errors.email }</p>
      }
      <input type="submit" />
    </form>
  );
}

Other Hooks

Please see all of the other reacthooks.org hooks:

Author

$ npx chilts

   ╒════════════════════════════════════════════════════╕
   │                                                    │
   │   Andrew Chilton (Personal)                        │
   │   -------------------------                        │
   │                                                    │
   │          Email : andychilton@gmail.com             │
   │            Web : https://chilts.org                │
   │        Twitter : https://twitter.com/andychilton   │
   │         GitHub : https://github.com/chilts         │
   │         GitLab : https://gitlab.org/chilts         │
   │                                                    │
   │   Apps Attic Ltd (My Company)                      │
   │   ---------------------------                      │
   │                                                    │
   │          Email : chilts@appsattic.com              │
   │            Web : https://appsattic.com             │
   │        Twitter : https://twitter.com/AppsAttic     │
   │         GitLab : https://gitlab.com/appsattic      │
   │                                                    │
   │   Node.js / npm                                    │
   │   -------------                                    │
   │                                                    │
   │        Profile : https://www.npmjs.com/~chilts     │
   │           Card : $ npx chilts                      │
   │                                                    │
   ╘════════════════════════════════════════════════════╛

(Ends)

Package Sidebar

Install

npm i @reacthooks.org/use-form-validation

Weekly Downloads

0

Version

1.0.9

License

ISC

Unpacked Size

8.88 kB

Total Files

5

Last publish

Collaborators

  • chilts
  • chilts-appsattic