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

js-validations-form-localized

v1.0.4

Published

This package provides a validator function that can be used to perform validation checks on form inputs. It includes a set of rules that can be used to ensure that the values entered in the form meet the required criteria, such as being of a certain lengt

Downloads

24

Readme

js-validations-form-localized v1.0.4

Swiftly setup frontend form error handlers!

This package provides a validator function that can be used to perform validation checks on form inputs. It includes a set of rules that can be used to ensure that the values entered in the form meet the required criteria, such as being of a certain length or format. The function can be easily customized to fit the specific needs of your project, and it comes with translations for various languages such as English, French and Arabic...

TOC

Installation

yarn add js-validations-form-localized

or

npm install js-validations-form-localized

Usage

import { validate } from 'js-validations-form-localized';

const handleSubmitForm = async () => {
	try {
		const data = [
			{
				rules: ['email', 'min: 32', 'max:255'],
				value: emailInput,
				// overrides all rules default messages
				customMessages: "Email is wrong",
			},
			{
				rules: ['required', 'min: 6', 'max:32'],
				value: phoneInput,
				prefix: 'phone',
				// overrides rules[0] ('required') and rules[2] ('max:32') default messages 
				// while leaving rules[1] (min:6) with its default message
				customMessages: ['custom msg 1', null, 'custom message2']
			},
			{
				rules: ['equal'],
				value: [password, confirmPassword],
				prefix: ['password', 'confirm password'],
			}
		];
		validate(data, 'en');

		// continue with valid data
	} catch (e) {
		// handle error message
		console.log(e);
		alert(e.message);
	}
}

Parameters

| parameter | required | Default value | interface | | :---: | :---: | :---: | :----: | | data | YES | undefined | IValidations[] | lang | NO | 'en' | TAcceptedLanguages

Interfaces

Validations

interface IValidations {
	rules: 'required' | 'email' | 'equal' | 'notEqual' | `min:<number>` | `max:<number>` | `maxMB:<number>` | `minMB:<number>`,
	prefix?: string | string[],
	customMessages?: string | null | string[],
	value: (string | number) | (string | number)[]
}

Accepted languages

type TAcceptedLanguages = 'ar' | 'en' | 'fr'

Default Messages

| rule | en | fr | ar | | --- | --- | --- | ---- | | required | ${prefix} is required | ${prefix} مطلوب | ${prefix} est requis | email | wrong email format | mauvais format d'email | بريد إلكتروني غير صحيح | | max:3 (value is string) | ${prefix} should be at most 3 chars | ${prefix} devrait être au maximum 3 caractères | ${prefix} يجب أن يكون اقل من 3 احرف | | max:100 (value is number) | ${prefix} should be at most 100 | ${prefix} devrait être au maximum 100 | ${prefix} يجب أن يكون اقل من 100 | | min:3 (value is string) | ${prefix} should be at least 3 chars | ${prefix} devrait être au moins 3 caractères | ${prefix} يجب ان يكون اكثر من 3 احرف | | min:3 (value is number) | ${prefix} should be at least 3 | ${prefix} devrait être au moins 3 | ${prefix} يجب ان يكون اكثر من 3 | | maxMB:100 | ${prefix} should be at most 100 mb | ${prefix} devrait être au maximum 100 mo | ${prefix} يجب أن يكون اقل من 100 ميغابايت | | minMB:3 | ${prefix} should be at least 3 mb | ${prefix} devrait être au moins 3 mo | ${prefix} يجب ان يكون اكثر من 3 ميغابايت | | equal (no prefix given) | All of them should match | Ils devraient tous correspondre | يجب أن تتطابق جميعها | | equal (2 prefix given) | ${prefix[0]} should match with ${prefix[1]} | ${prefix[0]} devrait correspondre avec ${prefix[1]} | ${prefix[0]} يجب أن تتطابق مع ${prefix[1]} | | equal (multiple prefixes given) | ${prefix[0]}, ${prefix[1]}, ${prefix[2]}, ... should match | ${prefix[0]}, ${prefix[1]}, ${prefix[2]}, ... devrait correspondre | ${prefix[0]}, ${prefix[1]}, ${prefix[2]}, ... يجب أن تتطابق | | notEqual (no prefix given) | They should not be equal | Ils ne devraient pas être égaux | لا ينبغي أن يكونوا متساوين | | notEqual (2 prefix given) | ${prefix[0]} should not match with ${prefix[1]} | ${prefix[0]} ne doit pas correspondre avec ${prefix[1]} | ${prefix[0]} لا ينبغي أن تتطابق مع ${prefix[1]} | | notEqual (multiple prefixes given) | ${prefix[0]}, ${prefix[1]}, ${prefix[2]}, ... should not be equal | ${prefix[0]}, ${prefix[1]}, ${prefix[2]}, ... ne devrait pas être égal | ${prefix[0]}, ${prefix[1]}, ${prefix[2]}, ... لا ينبغي أن تكون متساوية |