guards.js

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

Named Guards

allow

available since: 1.0.0

Only values found in the given list are considered valid. Anything else triggers a failure. This guard requires an array parameter of the valid values.


Allowed values: red, yellow, blue

As of version 1.3.1, the allowed words can be specified via an object with a string property named words delimited by spaces by default, or delimited with the delimiter property. This is primarily to support the data attributes form of guards.


Allowed values: red, yellow, blue


Allowed values: United States, Canada

always

available since: 1.0.0

Always fail, no matter what. For this guard to pass, either the guard must be removed, or the element(s) guarded must be removed. No parameters are accepted.


Always fails, no matter what

different

available since: 1.0.0

This is a grouped guard where every field must have a different value.



Each value must be unique

disallow

available since: 1.0.0

Guard against specific values. This guard requires an array parameter of the invalid values.


Disallowed values: red, yellow, blue

As of version 1.3.1, the disallowed words can be specified via an object with a string property named words delimited by spaces by default, or delimited with the delimiter property. This is primarily to support the data attributes form of guards.


Disallowed values: red, yellow, blue


Disallowed values: United States, Canada

dateUS

available since: 1.1.0

Guard for a US date. Month and day value validations exist, though may not conver all invalid dates (so, 13/32/2013 is invalid, but 2/31/2013 might not properly fail at this point in time). An empty value is considered valid.


US date of any value

timeUS

available since: 1.1.0

Guard for a US time. Military time is not currently supported. An empty value is considered valid.


US time of any value

email

available since: 1.0.0

Guard for a valid email address. An empty value is ignored, so only once a value exists will this guard start checking for an email address. An optional argument of { allowDisplay: true } is allowed that may specify whether display emails of the form John Doe <john@example.com> are allowed.


Email address of the form "john@example.com"


Email address of the form "John Doe <john@example.com>"

float

available since: 1.0.0

Guard for a floating point number. Optionally, an object parameter may be passed with min and/or max. Min will restrict the minimum value, while max restricts the maximum. An empty value is considered valid.


A number of any value


A number no smaller than -5.5


A number no bigger than 42


A number from 0 to 10

int

available since: 1.0.0

Guard for an integer number. Optionally, an object parameter may be passed with min and/or max. Min will restrict the minimum value, while max restricts the maximum. An empty value is considered valid.


An integer of any value


An integer no smaller than -5


An integer no bigger than 42


An integer from 0 to 10

moneyUS

available since: 1.0.0

Guard for a US dollar amount. Optionally, an object parameter may be passed with min and/or max. Min will restrict the minimum value, while max restricts the maximum. An empty value is considered valid.


US money of any value


US money no smaller than -$5.50


US money no bigger than $42.02


US money from $0 to $10

never

available since: 1.0.0

Never fail, no matter what. For this guard to fail, it must be manually triggered via guard.triggerError(selector). This guard can be useful for marking a field as having an error immediately when the page loads (such as for a server detected error).


Never fails, except manually

oneRequired

available since: 1.0.0

This is a grouped guard where a single field of all the selected fields must have a value.



One value is required

phoneUS

available since: 1.0.0

The guarded field is considered valid if no value is given, or if the value given appears to be a valid US phone number. The number must include an area code. Whitespace is ignored.


A valid US phone number like (555) 555-1234

regex

available since: 1.2.0

These guarded fields must match the provided regex to pass. An empty value is considered valid.


abc with 1-3 digits is required, like 'abc123'

As of version 1.3.1, the regex can be specified via an object with a string property named pattern. This is primarily to support the data attributes form of guards.


abc with 1-3 digits is required, like 'abc123'

required

available since: 1.0.0

These guarded fields must have a value to pass. Only whitespace is not considered a value. If no named or custom guard is defined, this is the default guard used.


A value is required


A value is required

same

available since: 1.0.0

This is a grouped guard where every field must have the same value. For example, this guard can be used to implement a password confirmation field.



Each value must be the same

string

available since: 1.0.0

Validate the length of the string provided. This requires an object parameter with min and/or max. Min will restrict the minimum length, while max restricts the maximum length.


A string with at lease 3 characters


A string with no more than 7 characters


A string with at least 2 characters and no more than 5