项目作者: aalfiann

项目描述 :
Native JavaScript Form Validation
高级语言: JavaScript
项目地址: git://github.com/aalfiann/native-form-validation.git
创建时间: 2019-10-27T19:26:00Z
项目社区:https://github.com/aalfiann/native-form-validation

开源协议:MIT License

下载


Native Form Validation

NPM

npm version
CircleCI
Known Vulnerabilities
License
NPM download/month
NPM download total

Native JavaScript Form Validation for Browser / UI Framework.

Background

There is a lot of Form Validation, but most of them was created for jQuery and too bloated. This is an native javascript form validation which is can be use for all kind of javascript UI framework.

Install using NPM

  1. $ npm install native-form-validation
  2. // load using require in nodejs
  3. const FormValidation = require('native-form-validation');
  4. // or load using import in typescript
  5. import FormValidation from 'native-form-validation';
  6. // or load use with path for client side
  7. <script src="node_modules/native-form-validation/dist/formvalidation.min.js"></script>

Or simply use with CDN

  1. <!-- Always get the latest version -->
  2. <!-- Not recommended for production sites! -->
  3. <script src="https://cdn.jsdelivr.net/npm/native-form-validation/dist/formvalidation.min.js"></script>
  4. <!-- Get minor updates and patch fixes within a major version -->
  5. <script src="https://cdn.jsdelivr.net/npm/native-form-validation@1/dist/formvalidation.min.js"></script>
  6. <!-- Get patch fixes within a minor version -->
  7. <script src="https://cdn.jsdelivr.net/npm/native-form-validation@1.4/dist/formvalidation.min.js"></script>
  8. <!-- Get a specific version -->
  9. <!-- Recommended for production sites! -->
  10. <script src="https://cdn.jsdelivr.net/npm/native-form-validation@1.4.0/dist/formvalidation.min.js"></script>

Usage

  1. var FV = new FormValidation();
  2. // Create the rules
  3. FV.rules({
  4. username: {
  5. required: true,
  6. message: 'Username must be min 3-20 chars!',
  7. minLength:3,
  8. maxLength:20,
  9. regex: /^[a-zA-Z0-9]/,
  10. errorPlace:'username-error',
  11. errorAddClass: {
  12. username_group:'has-danger',
  13. username:'is-invalid'
  14. }
  15. },
  16. email: {
  17. regex: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
  18. errorPlace:'email-error',
  19. errorAddClass: {
  20. email_group:'has-danger',
  21. email:'is-invalid'
  22. }
  23. }
  24. });
  25. // Validate all
  26. FV.validate();
  27. // Determine is Valid all
  28. if(FV.isValid()) {
  29. // run your code
  30. }
  31. // Validate per element
  32. FV.element('username').validate();
  33. // Determine is Valid per element
  34. if(FV.element('username').isValid()) {
  35. // run your code
  36. }

Documentation

Please see our Wiki at here.