npm.io
1.0.2 • Published 3 years ago

form-part-validator

Licence
ISC
Version
1.0.2
Deps
0
Size
17 kB
Vulns
0
Weekly
0

FORM PART VALIDATOR

The validator allows you to specify easily what each input should contain to be valid you can then watch for errors :

import Validator from "./validator.js";

const validator = new Validator();
validator.watch(document.querySelectorAll("input"));

you simply add a keyword to it's data-form-constraints attribute, separated by spaces :

<input type="text" name="foo" data-form-constraints="length:0:38 nodot nowhitespace nonumber">

this will, when using the watch function, produce an error badge when the input doesn't match it's constraints

CONSTRAINTS

name function usage
length assert length between min and max lengthmax
minLength assert minimum length minLength:min
maxLength assert maximum length maxLength:max
text assert value contains some text text
uppercase assert value is uppercase uppercase
lowercase assert value is lowercase lowercase
number assert value is numbers number
nodot assert value has not dots nodot
nowhitespace assert value whitspace nowhitespace
nonumber assert value contains no numbers nonumber
email assert value looks like an email email
dateInPast assert the date is x yearsdays in the past dateInPastmonth:day
dateInFuture assert the date is x yearsdays in the future dateInPastmonth:day

MUST HAVE

the name attribute is necessary for error messages to work properly