Skip to content

Flexible and simple form validator and autocorrector for front-end and back-end.

License

Notifications You must be signed in to change notification settings

fermmm/simple-validator

Repository files navigation

simple-validator.js

This library validates content from strings and throws validation problems, is ment to be used to validate input fields on forms. Returns autocorrected version and specific position of the problem to implement a visual underline (this is a unique feature from this package). You pass a string and the library returns an object with the information about the problems and the autocorrected version. Implemented in Typescript with no dependencies, can be used also in Node.js.

Supported languages for problem descriptions: English and Spanish.

Features:

  • Simple: only needs a string (the text of the input field)
  • No dependencies
  • Returns a specific description of the validation problem
  • Returns the position of the validation problem in the string (to implement a red underline)
  • Returns an autocorrected version of the string (to implement autocorrect while typing)
  • You build the rules of the validation by chaining functions (see below)
  • Created in Typescript
  • Tested in production and improved based on users feedback on many froms in bondacom.com
  • Fully documented with jsdoc and TypeScript, you should see documentation with autocomplete

Getting Started

To install run:

npm i simple-validator-js

Everything is inside the "Validation" class, you need to chain functions, example:

import {Validation} from 'simple-validator-js';

const name = "roberto666";
const validationInfo = new Validation(name)
                        .noNumbersAllowed(false)    // We set autocorrect to false for this rule to see how errors are returned.
                        .noSpacesAllowed()
                        .noSpecialCharactersAllowed(true, null)
                        .maxChars(100)
                        .minWords(1)
                        .maxWords(1)
                        .wordsShouldStartWithUpperCase();

console.log(validationInfo.result);

Prints the following object:

{
    text: "Roberto666",             // Notice the uppercase first letter was auto-corrected and this is the correct version.
    textOriginal: "roberto666",
    isValid: false,                 // If isValid is false it means there are errors even after auto-correcting.
    errors: [
                {
                    description: {en: "numbers are not allowed.", es: "no se permite escribir números."},
                    locations: [7, 8, 9]
                }
            ]
}

Library configuration

The object returned has errors descriptions, the error texts are sent in all the languages avaiable in this tool, to return an error object with only one language call this (before any other call):

Validation.languageFilter = LanguageFilter.english;

With the language you want. This is useful to implement your own translation logic based on the english language.

Donate

If you like this library and want it to become something even better, please donate here.

Documentation:

Methods

customRegex(regex: RegExp, invert: boolean, name: string): Validation

With this filter you apply a custom regex. Useful in complex validations. The drawback is that this does not return useful

information in the error text and does not support auto correct.

Parameters:

regex | RegExp | - | Regex expression. |

invert | boolean | false | To switch, so the regex allows or dissallows the validation. |

name | string | "" | This will be showed in the error as the first word before "is invalid", example: content is invalid. |


emailConfirmation(email: string | function): Validation

Checks if the text is the same than the text passed to this function, if the texts does not match creates a "email not matching" error.

Parameters:

email | string | function | It can be a string with the email to check for matching, also can be a function that returns a string. |


isEmail(): Validation

Email validator rule. Does not report error details, only if it's valid or not.


maxChars(charsAmount: number, autocorrect: boolean): Validation

Maximum of characters that should be written.

Parameters:

charsAmount | number | - | - |

autocorrect | boolean | false | In this case autocorrect default value is false because when the user writes more than what is needed he/she needs to be aware of what is happening otherwise it looks like the input field is bugged. |


maxWords(wordsAmount: number, autocorrect: boolean): Validation

Max amount of words that should be written.

Parameters:

wordsAmount | number | - |

autocorrect | boolean | true |


minChars(charsAmount: number): Validation

A minimum of characters that should be written.

Parameters:

charsAmount | number | The amount of minimum characters allowed. |


minWords(wordsAmount: number): Validation

Minmum amount of words that should be written.

Parameters:

wordsAmount | number |


noInvalidSpacesAllowed(autocorrect: boolean): Validation

Invalid spaces means when the first character is a space or when there is one space after the other.

Parameters:

autocorrect | boolean | true | Autocorrect removing concatenated spaces and first space or the problem should be returned as an error. |


noLettersAllowed(autocorrect: boolean): Validation

Parameters:

autocorrect | boolean | true |


noNumbersAllowed(autocorrect: boolean): Validation

Parameters:

autocorrect | boolean | true |


noSpacesAllowed(autocorrect: boolean): Validation

Parameters:

autocorrect | boolean | true |


noSpecialCharactersAllowed(allowUnicode: boolean, exceptions: string[], autocorrect: boolean): Validation

This validator only allows alphanumeric characters by dafault (only letters and numbers), you can specify exceptions.

Parameters:

allowUnicode | boolean | true | If you have a string like "Âlvarö", it would reject it because of the letters  and ö unless this is true (default). |

exceptions | string[] | null | A list of characters that are allowed. |

autocorrect | boolean | true | - |


passwordConfirmation(password: string | function): Validation

Checks if the text is the same than the text passed to this function, if the texts does not match creates a "password not matching" error.

Parameters:

password | string | function | It can be a string with the password to check for matching, also can be a function that returns a string. |


shouldContain(characterOrText: string, maxAllowed: number): Validation

Parameters:

characterOrText | string | - |

maxAllowed | number | null |


shouldContainAfter(characterOrText: string, referenceCharacterOrText: string, minDistance: number): Validation

When a character or text inside string should be present after another one.

Parameters:

characterOrText | string | - | The character or text that should be present after another one. |

referenceCharacterOrText | string | - | The character or text that should be before. |

minDistance | number | 1 | The minimum index dinstance from one to the other. Shoud be a value higher than 1. |


shouldContainAt(textOrCharacter: string, positionIndex: number, fromTheBeginning: boolean, autocorrect: boolean): Validation

When a text or character should be in a specific position.

Parameters:

textOrCharacter | string | - | The text or character that should be in a specific position. |

positionIndex | number | - | The position index in the string in where the text or character should be at. |

fromTheBeginning | boolean | true | If the position index should be counted from the beggining or from the bottom of the string. Warning: Autocorrect does not work if this is false. |

autocorrect | boolean | true | Autocorrect adding the text or character or if it should be returned as an error. If fromTheBeginning is false this does not work. |


shouldContainBefore(characterOrText: string, referenceCharacterOrText: string, minDistance: number): Validation

When a character or text inside string should be present before another one.

Parameters:

characterOrText | string | - | The character or text that should be present before another one. |

referenceCharacterOrText | string | - | The character or text that should be after. |

minDistance | number | 1 | The minimum index dinstance from one to the other. Shoud be a value higher than 1. |


shouldEndWith(textToMatch: string): Validation

Parameters:

textToMatch | string |


shouldStartWith(textToMatch: string, autocorrect: boolean): Validation

Parameters:

textToMatch | string | - |

autocorrect | boolean | true |


wordsShouldStartWithUpperCase(autocorrect: boolean): Validation

This validator only evaluates the first character of each word: If it's a letter in upper case is valid, if

it's not a letter is valid, if it's a letter in lowercase is invalid.

Parameters:

autocorrect | boolean | true |

Returns: Validation

About

Flexible and simple form validator and autocorrector for front-end and back-end.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published