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

validate-form-p

v1.2.0

Published

validator

Downloads

87

Readme

validate-form-p

English | 简体中文

validator

NPM

In a lot of the front-end logic, we'll get into the form validation part of the logic.

Abstracted out, can let us reduce workload greatly

Usage

SAMPLE DEMO

import validator from 'validate-form-p'

const rules = [
  ['name', 'require', 'The name has to exist']
]

const data = {
  name: 'leo'
}

validator.setData(data).validate(rules) // ture
import validator from 'validate-form-p'

const rules = [
  ['name', 'require', 'The name has to exist']
]

const rules1 = [
  ['name', 'require', 'The name has to exist', 1] // 1: You have to check whether the name field is in the data or not
]

const data = {
  phone: ''
}

validator.setData(data).validate(rules) // ture
validator.setData(data).validate(rules1) // false
console.log(validator.getError()) // { name: The name has to exist }
// maybe you want to do : 
// Toast.info(Object.values(validator.getError()).join(','))
import validator from 'validate-form-p'
// Define rules

const formData = {
  name: "xiaoming",
  number: "2"
};

const rules = [
  ["name", "require", "The name is empty"],
  ["name", "xiaoming", "My name is xiaoming", 0, "equal"],
  [
    "number",
    "2",
    "The value can't be 2.",
    0,
    (value, secondIndexValue) => {
      return value !== secondIndexValue;
    }
  ]
];

// Set the data to be validated
// Then verify that it returns true or false
const result = validator.setData(formData).validate(rules)
// If true, the form can be submitted directly
console.log(result)
// If false is returned, an error message can be obtained by getError()
console.log(validator.getError())

Rules

Validation fields, validation rules, error messages,[validation conditions, additional rules]

validation conditions (options)

|Value | trigger condition| |---|----| |0|Validate if a field exists (default)| |1|Must be validated| |2|Verify when the value is not empty|

additional rules (options)

|rules|explain| |----|----| |regex|Regular validation. The validation rule defined is a regular expression (default)| |function|Function validation. The validation rule defined is a function name| |confirm|To verify that two fields in a form are the same, the validation rule defined is a field name| |equal|Verifies that it is equal to a value defined by the previous validation rule| |notEqual|Verifies that does not equal a value defined by the previous validation rule| |in|To verify that a range is defined, the validation rule can be an array or a comma-separated string| |notIn|To verify that the validation rule is not in a range, the defined validation rule can be an array or a comma-separated string| |length|Validation length. The defined validation rules can be a number (representing a fixed length) or a number range (for example, 3,12, representing a length range from 3 to 12)|

Try

codesanbox.io: https://codesandbox.io/embed/festive-field-k66vh

License

MIT

Postscript

import you, { star } from 'you'
import me, { thank } from 'me'

star(me) && thank(you)