-
Notifications
You must be signed in to change notification settings - Fork 23
Built In Tests
ApproveJs ships with various tests built in to get you started right away.
All tests return a default object as a result.
Checks if the value is not empty or null or undefined.
Parameters: none
Result: default
Message: '{title} is required'
var rule = {
required: true
};
var result = approve.value('value', rule);
Checks if the value is a valid email address.
Parameters: none
Result: default
Message: '{title} must be a valid email address'
var rule = {
email: true
};
var result = approve.value('[email protected]', rule);
Check if the value is a valid web address.
Parameters: none
Result: default
Message: '{title} must be a valid web address'
var rule = {
url: true
};
var result = approve.value('http://www.domain.com', rule);
Checks if the value is a valid credit card number.
Parameters: none
Result:
{
**default**
scheme: string
}
scheme
: The credit card scheme i.e. 'American Express'
Message: '{title} is not a valid credit card number'
var rule = {
cc: true
};
var result = approve.value('000000000000000', rule);
Checks if the value contains both letters and numbers.
Parameters: none
Result: default
Message: '{title} may only contain [A-Za-z] and [0-9]'
var rule = {
alphaNumeric: true
};
var result = approve.value('Some Text 123', rule);
Checks if the value is a number.
Parameters: none
Result: default
Message: '{title} may only contain [0-9]'
var rule = {
numeric: true
};
var result = approve.value(123, rule);
Checks if the value contains only letters.
Parameters: none
Result: default
Message: '{title} may only contain [A-Za-z]'
var rule = {
alpha: true
};
var result = approve.value('Some Text', rule);
Checks if the value is a valid decimal.
Parameters: none
Result: default
Message: '{title} must be a valid decimal'
var rule = {
decimal: true
};
var result = approve.value(10.50, rule);
Checks if the value is a valid currency value.
Parameters: none
Result: default
Message: '{title} must be a valid currency value'
var rule = {
currency: true
};
var result = approve.value(9.99, rule);
Checks if the value is a valid ipv4 or ipv6 address.
Parameters: none
Result: default
Message: '{title} must be a valid IP address'
var rule = {
ip: true
};
var result = approve.value('192.16.118.25', rule);
Checks if the value has a minimum of n characters.
Parameters:
-
min
: minimum length
Result: default
Message: '{title} must be a minimum of {min} characters'
var rule = {
min: 5
};
var result = approve.value('Some Text', rule);
Checks if the value has a maximum of n characters.
Parameters:
-
max
: maximum length
Result: default
Message: '{title} must be a maximum of {max} characters'
var rule = {
max: 20
};
var result = approve.value('Some Text', rule);
Checks if the value's character length is between a minimum and maximum.
Parameters:
-
min
: minimum length -
max
: maximum length
Result: default
Message: '{title} must be a minimum of {min} and a maximum of {max} characters'
var rule = {
range: {
min: 5,
max: 20
}
};
var result = approve.value('Some Text', rule);
Checks if the value equals another value.
Parameters:
-
value
: The other value to compare with. -
field
: The name of the other value for correctly formatting errors.
Result: default
Message: '{field} must be equal to {title}'
var rule = {
title: 'Password'
equal: {
value: 'Other text',
field: 'Confirm Password'
}
};
var result = approve.value('Some Text', rule);
Checks if the value passes a custom regular expression.
Parameters:
-
regex
: a regular expression to test against
Result: default
Message: '{title} did not pass the [{regex}] test'
var rule = {
format: /^[A-Za-z]+$/
};
var result = approve.value('Some Text', rule);
Checks if the value is a strong password like string.
It checks based on a 6 point score:
- value >= minimum required length ? +1 point
- value contains at least one lowercase character ? +1 point*
- value contains at least one uppercase character ? +1 point*
- value contains at least one numeric character ? +1 point*
- value contains at least one special character ? +1 point*
- value >= minimum bonus length ? +1 point*
*Point is only awarded if value >= minimum length.
Parameters:
-
min
: The minimum length a password must be. Default8
. -
bonus
: The minimum length a password must be, higher thanmin
for a bonus point. Default10
.
Options:
-
messages
: The default error messages for the password score.-
isMinimum
:'{title} must be at least {min} characters'
-
hasLower
:'{title} must have at least 1 lower case character'
-
hasUpper
:'{title} must have at least 1 upper case character'
-
hasNumber
:'{title} must have at least 1 number'
-
hasSpecial
:'{title} must have at least 1 special character'
-
Result:
{
**default**
strength: string,
points: integer,
isMinimum: boolean,
hasLower: boolean,
hasUpper: boolean,
hasNumber: boolean,
hasSpecial: boolean,
isBonus: boolean,
percent: integer
}
-
result.strength
: The strength of the string based on points.- 0: 'Very Weak',
- 1: 'Weak',
- 2: 'Better',
- 3: 'Almost',
- 4: 'Acceptable',
- 5: 'Strong',
- 6: 'Very Strong'
-
result.points
: The calculated points -
result.isMinimum
: Length >= minimum -
result.hasLower
: Has at least one lower case character -
result.hasUpper
: Has at least one upper case character -
result.hasNumber
: Has at least one number -
result.hasSpecial
: Has at least one special character -
result.isBonus
: Length >= bonus length -
result.percent
: The strength percentage (%) 0 to 100
Message: '{title} did not pass the strength test.'
var rule = {
strength: {
min: 8,
bonus: 10
}
};
var result = approve.value('Some Text 123', rule);
You can pass multiple tests with your rules object:
var rule = {
required: true,
format: /^[A-Za-z]+$/,
min: 5,
max: 20
};