npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-form-validator

v1.1.0

Published

a simple simple-form-validator, inspired by Backbone validation plugin

Downloads

38

Readme

Simple-Form-Validator

A simple form validator is easy, flexible to use

Installation

npm install --save simple-form-validator

This assumes that you’re using npm package manager with a module bundler like Webpack or Browserify to consume CommonJS modules.

Documentation

  • [ createValidators] initialize validators with validator settings

    1. [validator setting can be an object describing validators for form field] each validator object must have two keys, one of which should be msg , the other of which could be required, length, sameas, options, pattern, match and is case-sensitive, moreover, value for these keys could be a function
    var validatorsConfig = {
        name: [{
            required: true,
            msg: 'name cannot be empty'
          }, {
            pattern: 'string',
            msg: 'name should be string'
          }],
        age: [
          {
            required: true,
            msg: 'age should be provided'
          }, {
            pattern: 'digits',
            msg: 'age should be digits'
          }
        ]
      }
    validators = createValidators(validatorsConfig);
    
    1. [validator setting can also be collections of either built-in validators or customized validators for each form field]
    validators = createValidators({
    income: [
        builtinValidators.isRequired('income should be given'), 
        createValidator({
          msg: 'amount is invalid',
          validate: (val, contextFields) => /^\d+[,.]?\d*$/.test(String(val))
        }), 
        builtinValidators.range('income should be between 0 and 100000000', 0, 100000000)
      ],
    address:[
      builtinValidators.isString('valid address should be string'), 
      builtinValidators.length(5, 'valid address should be at least 5 letters')
      ]
    })
    validators = createValidators(validatorsConfig);
  • [addValidator] After a form validators is initialized, it is able to dynamically add validators to an existing form field or a brand new field, i.e. (see more example in validate.test.js)

    validators.addValidator('employer', builtinValidators.isRequired('employer should be provided', function() {
    if (emp === 'student') return false;
    else if (emp === 'developer') return true;
      }))
  • [validateField] When a form field has changed its contents or lost focus, validateField can be called to validate it. If its value doesn't satisfy all rules of its validators, the corresponding validating error message will be returned. To return undefined means there is no validating errors. It can be called with an optional options that could contain contextFields, implicityRequired and "stopOnErr"

    /* validate a form field with the current value
    @param contextFields        a collection of form feilds and their corresponding values in the concerned form where this field resides 
    @param stopOnErr            flag designating if the validating process should stop when the first validating error appears, default value is TRUE
    @param implicityRequired    flag designating if a field is mandatory if a validator is provicded, default value is TRUE
    */
    validateField: function(field, value, { contextFields={}, stopOnErr=false, implicitRequired=true }={})
    {
    
    }
  • [validateForm] validateForm can be called to validate an entire form or just a part of it. Imagine that you have a Accordion showing a complex form and navigate from part 1 to part 2, and part 1 should validated before part 2 is to be shown. Return value of undefined for this method means there is no validating errors. It can be called with an optional options that could contain implicityRequired and "stopOnErr"

    /* validate a form or a part of it
    @param formData             a collection of entire or a part of form feilds that should be validated
    @param stopOnErr            flag designating if the validating process should stop when the first validating error appears, default value is TRUE
    @param implicityRequired    flag designating if a field is mandatory if a validator is provicded, default value is TRUE
    */
    validateField: function(formData, { stopOnErr=false, implicitRequired=true }={})
    {
    
    }
  • [built-in validators] for the time being, there are nine built-in validators, isRequired, isNumber, isSame, isString, isEmail, range, length, options, match. The last one is provided for your customized need because you can send your own javascript RegExp

  • [customized validators] Besides the above mentioned built-in validators, it is possible to create your own customized validators by calling createValidator, which takes in a js object as param, consisting of msg as validating error msg, validate function, called with (val, contextFields), for instance

    formvalidators = createValidators({
        amount: [
            builtinValidators.isRequired('income should be given'), 
            createValidator({
              msg: 'amount is invalid',
              validate: (val, contextFields) => /^\d+[,.]?\d*$/.test(String(val))
            })
          ]
        })

The customized validators can be used in the same way as built-in validators

Other Usage

In additon to its usage mentioned above, simple-form-validator is originally designed for being used together with react-form-ex, which is a ReactForm provider, faciliating form validation with React components

Open Source Code

Source code for this npm package is available idavollen@github

Enjoy!

License

MIT