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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mp-form-validator

v1.0.11

Published

Simple form validation

Readme

mp-form-validator

It's just a simple form validator with hooks, custom rules, translations, overriding functions.

Bundle size: 7,57KB

In Development

Installation

npm i mp-form-validator

Usage

import Validator from "mp-form-validator"

or

import "mp-form-validator/bundle/validator.bundle.min.js"

or

<script src="//unpkg.com/[email protected]/bundle/validator.bundle.min.js"></script>

Next, create new Validator instance:

var v = new Validator({...params});

Params object:

|property| value type | desc | |:---:|:---:|:---:| | debug | boolean | Show/hide debug messages in console | | errorWrapperClass | string | Class name for errors wrapper | | errorClass | string | Class name of single error | | errorPosition | string ('before' or 'after') | Where place a error - before or after input | | language | string | Lang code, if not found load default english. | | constraints | object | Object of constraints | | translations | object | Object of translations. Passed override language passed above. |

Default params object:

var paramas = {
    debug: false,
    errorWrapperClass: 'form-errors-msgs',
    errorClass: 'single-error-msg',
    errorPosition: 'before',
    language: 'en',
    constraints: {},
    translations: {}
}

Methods

|method|param|return|desc| |:---:|:---:|:---:|:---:| | validateField|field,forceValue|boolean|validate single field| | validateForm|form|boolean|validate passed form| | isFormValid|-|boolean|check if form is valid| | removeAllErrorMessages|-|void|remove all added messages to form elements| | getErrors|-|object|get all form errors|

...where:

  • field - DOM element
  • form - DOM element
  • forceValue - string/boolean - sometimes value of your field is changing with delay (e.g. react setState() method, field is binded with state value, but metod works async and value of DOM element will be one change before - NOW you can pass directly field value and all works fine :))

Constraints

By default, Validator don't know, what you want to validate - so, let's go!

Sample definition of constraints:

var constraints = {
    firstName: {
        required: true,
    },
    phone: {
        required: true,
        phone: true
    },
    email: {
        required: true,
        email: true
    }
};

var params = {
    ...params,
    constraints: constraints
}

firstName, phone, email are just name of form elements. Each form element must have a unique name.

Order of processing rules is just order of rules in your Constraint object - for field with name phone first will be 'required', second 'phone'.

...and rules

For now, we have only 8 rules available - yes - it's simple but powerful validator :>

| ruleName | description | | --- | --- | | required | check if length of field > 0; if checkbox or radio, check if input group has any checked values | | phone | check if have only digits and 9 numbers | | email | simple check email | | password | check if password has 1 lower, 1 upper, 1 numeric, 1 special and >= 8 chars | | pesel | check for polish pesel number | | regon | check for polish regon number | | nip | check for polish nip number | | idCard | check for polish idCard number |

Custom rules

Yes - you can define your own logic for rules.

For example - you want to validate if entered text >= 10 characters.

First, define your rule:

var rules = {
    'text-length': {
        valid: function(value, field) {
          return value.length >= 10;
        }
    }
}

...and define translation (if invalid):

var translations = {
    Rules: {
        Errors: {
            'text-length': 'You should enter min. 10 chars or greater'
        }
    }
}

...and pass it to your params:

var params = {
    ...params,
    rules: rules,
    translations: translations
}

Translations

Create own translations object that should be like example below:

var translations = {
    Rules: {
        Errors: {
            "required": "This field is required.",
            "phone": "Incorrect phone format. Should be exact 9 digits.",
            "email": "Incorrect email address.",
        }
    }
}

and pass your object to params:

var params = {
    ...params,
    translations: translations
}

Hooks

Plugin have some hooks inside. You can override default hook, by passing to params object, a function definition:

| functionName | params | |:---:|:---:| |onFieldValidateError|field,value,errorMessages| |onFieldValidateSuccessful|field, value| |onFormValidationError|form, errorMessages| |onFormValidationSuccessful|form| |beforeFieldValidation|field| |afterFieldValidation|field| |beforeFormValidation|form| |afterFormValidation|form|

...where:

  • field - DOM element
  • form - DOM element
  • value - string/boolean with field value
  • errorMessages - object with errors, where key is a field name, ale value is an array of validation messages

Sample usage:

var params = {
    ...params,
    onFormValidationError: function(form, errorMessages) {
      form.querySelector('.btn-submit').classList.add('disabled')
    },
    onFormValidationSuccessful: function(form) {
      form.querySelector('.btn-submit').classList.remove('disabled')
    }
}

Notice:

  • There's no auto-validation on submit, you have full control over when & how. More in index.html

Sorry for everything - it's my first package, if you found a bug, report - thanks!

License

ISC