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

neko-formvalidator

v3.0.0

Published

Create and customize form validation rules

Downloads

3

Readme

Form Validator powered by NekoDev

Validator is an extensible class for form validation.

Getting Start


import Validator, { Required, Numeric, Min } from 'FormValidator';

Validator.use([Required, Numeric, Min])

let form = document.getElementById('form-test');
let validator = new Validator(form);

form.addEventListener('submit', function (e) {
    e.preventDefault()
    let results = validator.validate({
        'name': 'required',
        'age': 'required|numeric|min:18',
    });

    console.log(results)
})

You can chain rules with a pipe like this :

'age': 'required|numeric|min:18'


Using a validation rule

For using a validation rule, you need import it :

import Validator, { Required, Max, Min } from 'FormValidator'

Validator.use(Required)
Validator.use(Max)
Validator.use(Min)

You can inline the user method like this :

Validator.user([ Required, Min, Max ])

For debugging, you can console.log the Rule description :

console.log(Required.Description)

You can customize the Rule error message :

Required.ErrorMessage = "My custom message"

Create a custom validation rule


import { Rule } from "FormValidator";

export default class CustomRule extends Rule {

    static Description = `La valeur du champs doit être "TEST".` // Developper Help Message
    static ErrorMessage = `La valeur du champs est incorrect !` // Error Message

    constructor() {
        super('customRule') // Rule name
    }

    applyRule() {
        this.setErrorMessage(CustomRule.ErrorMessage)
        if (this.input != "TEST") return this.error()
    }

}

A validation rule must extends of Rule class.
In the constructor, your must call super method ans pass the custom rule name.
It's recommanded to set startics Description and ErrorMessage.
The applyRule method contains the logic of your validation rule. In the applyRule method, call the error method when the rule fail.


Create custom validation with paramters


import { Rule } from "FormValidator";

export default class CustomRule extends Rule {

    static Description = `La valeur du champs doit être égale à la valeur passé en paramètre.` // Developper Help Message
    static ErrorMessage = `La valeur du champs est incorrect !` // Error Message

    constructor() {
        super('customRule') // Rule name
    }

    applyRule(params) {
        this.setErrorMessage(CustomRule.ErrorMessage)
        if (this.input != params) return this.error()
    }

}

On the front you can use the rule like this :


import Validator from 'FormValidator'
import CustomRule from './Rules/CustomRule'

Validator.use(CustomRule)

let validator = new Validotor(document.getElementById('my-form'))

let results = validator.validate({
    name: 'customRule:TEST'
})

The value expected in this exemple is the content after the :. customRule is the name of the rule and all after the : is the value expected (TEST).


Default Rule list

| Rule | Description | Exemple | |---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------| | Required | The input cannot be null | 'name' : 'required' | | Required_if | The input cannot be null if the input in parameter is not null or true | 'name' : 'required_if:newsletter' | | Required_with | The input cannot be null if all input passed in parameter are not null or true | 'name' : 'required_with:address,phone' | | Min | If the input is a string, the length must be greeter or equal by the value in parameter. If the input is a number, the value must be greeter or equal by the value in parameter. | 'password' : 'min:8', 'age' : 'min:18' | | Max | If the input is a string, the length must be lower or equal by the value in parameter. If the input is a number, the value must be lower or equal by the value in parameter. | 'password' : 'max:8', 'age' : 'max:18' | | Numeric | The input must contain a numeric value (even if the type of the input is text) | 'age' : 'numeric' | | Email | The input must be a valid email | 'email' : 'email' | | Pattern | The input value must match with the regex pattern pass in parameter | 'username' : '/^Neko_/' |

You can add nullable rule for input not required but if not null, must match with a specific rule :


form.validate({

    'age' : 'numeric|min:18|nullable'

})

Powered with :heart: By NekoDev !

Follow me on Twitter