guards.js

View the Project on GitHub on-site/guards.js

Options

Numerical guards

Some guards can take options which affect how the guard behaves. The "int", "float", and "moneyUS" guards can have min, max or min and max options which determine the acceptable range of those inputs. Note that these numeric guards (along with most guards) will pass if no input is provided. You must use the required guard along with them if you want no input to cause an error. There is no problem using multiple guards against a single field. Try out the numeric guards in the forms below:

int


minimum 5


maximum 15


minimum -5, maximum 42

float


minimum -5.3


maximum 42.42


minimum 0, maximum 0.999

moneyUS


minimum -$0.42


maximum $50.50


minimum $15, maximum $50

Strings

The "string" guard can be used to validate the length of the user's provided input. This checks the length against the provided min and/or max options. This behaves different from most other guards besides "required" in that empty input can trigger an error. Whitespace at the beginning or end of the input is ignored with respect to the validated length. Try out the guard below:

string


minimum length 2


maximum length 5


minimum length 2, maximum length 4

Allow and disallow

If you need to block all input except a few specific values, the "allow" guard is for you! You can list a bunch of inputs, and the guard will fail unless the input matches one of the values. If you need the opposite, and want to block specific inputs, then use the "disallow" guard. Try it out below:

allow


a color

disallow


not a color

previous next