项目作者: ederssouza

项目描述 :
Form validation in pure JavaScript
高级语言: JavaScript
项目地址: git://github.com/ederssouza/vanillajs-form-validator.git
创建时间: 2018-09-17T12:54:20Z
项目社区:https://github.com/ederssouza/vanillajs-form-validator

开源协议:MIT License

下载


VanillaJS Form Validator

Build Status Coverage Status npm version MIT License

Form validation in pure JavaScript.

Installation

  1. npm install vanillajs-form-validator --save

How to use

1 - Create HTML markup with data-required on <input /> tag:

  1. <form id="form" novalidate>
  2. <div class="form-group">
  3. <input
  4. type="text"
  5. name="name"
  6. placeholder="Name"
  7. autocomplete="off"
  8. data-required>
  9. </div>
  10. <button type="submit">Submit</button>
  11. </form>

2 - Create validator instance and call .init() method:

  1. var form = document.getElementById('form')
  2. var formValidate = new FormValidate({
  3. formSelector: form
  4. })
  5. formValidate.init()

Options

Properties

name type defalt value
formSelector HTML DOM Element
formGroupSelector String form-group
validClass String valid
invalidClass String invalid
msgClass String input-msg

Methods

name action
FormValidate.trigger() Force trigger validation event
FormValidate.checkValidFields() Check if form required fields was validated
FormValidate.reset() Clear form values and valid and invalid classes
FormValidate.getValues() Get form values and return an object

Validations rules attribute

Add data-required attribute on <input /> tag:

  1. <div class="form-group">
  2. <input
  3. type="email"
  4. name="name"
  5. placeholder="Name"
  6. autocomplete="off"
  7. data-required
  8. data-validate-rule="email">
  9. </div>
name validation type
data-validate-rule="email" Check valid e-mail
data-validate-rule="phone" Check valid Brazil phone number
data-validate-rule="cpf" Check valid CPF
data-validate-rule="rg" Check valid RG
data-validate-rule="cep" Check valid CEP
data-validate-rule="url" Check valid CEP

Regex attribute

Add data-validate-regex attribute on <input /> tag:

  1. <div class="form-group">
  2. <input
  3. type="text"
  4. name="name"
  5. placeholder="Only numbers"
  6. autocomplete="off"
  7. data-required
  8. data-validate-regex="\d+"
  9. data-validate-msg="Only numbers is required field">
  10. </div>

Custom error message

Add data-validate-msg attribute on <input /> tag:

  1. <div class="form-group">
  2. <input
  3. type="email"
  4. name="name"
  5. placeholder="Name"
  6. autocomplete="off"
  7. data-required
  8. data-validate-msg="Name is required field">
  9. </div>

Examples

Basic

Click here to view demo.

  1. <form id="form" novalidate>
  2. <div class="form-group">
  3. <input
  4. type="text"
  5. name="name"
  6. placeholder="Name"
  7. autocomplete="off"
  8. data-required>
  9. </div>
  10. <button type="submit">Submit</button>
  11. </form>
  12. <script src="dist/js/vanillajs-form-validator.js"></script>
  13. <script>
  14. var form = document.getElementById('form')
  15. var config = {
  16. formSelector: form
  17. }
  18. var formValidate = new FormValidate(config)
  19. formValidate.init()
  20. </script>

Rewriting HTML classes names

  1. var form = document.getElementById('form')
  2. var formValidate = new FormValidate({
  3. formSelector: form,
  4. inputGroupClass: 'uk-margin',
  5. validClass: 'uk-form-success',
  6. invalidClass: 'uk-form-danger',
  7. msgClass: 'input-msg'
  8. })
  9. formValidate.init()

Full example

Click here to view demo.

  1. var form = document.getElementById('form')
  2. var config = {
  3. formSelector: form,
  4. inputGroupClass: 'uk-margin',
  5. validClass: 'uk-form-success',
  6. invalidClass: 'uk-form-danger',
  7. msgClass: 'input-msg'
  8. }
  9. var formValidate = new FormValidate(config)
  10. formValidate.init()
  11. form.addEventListener('submit', () => {
  12. // check valid form
  13. var isValid = formValidate.checkValidFields()
  14. if (isValid) {
  15. // get input values
  16. console.table(formValidate.getValues())
  17. // reset form
  18. formValidate.reset()
  19. }
  20. })

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details