Data validations
Quick reference
Consider the following example:
Only numbers between 5 and 50
<html>
<script src="https://jsuites.net/v4/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v4/jsuites.css" type="text/css" />
<input id="example-input"/>
<script>
var validationParameters = {
constraint: 'between',
reference: [5, 50]
}
var exampleInput = document.getElementById('example-input');
exampleInput.addEventListener('input', function() {
var validationResult = jSuites.validations.number(exampleInput.value, validationParameters);
exampleInput.style.color = validationResult ? '#64DD17' : '#FF1744';
});
</script>
</html>
General explanation
For basic use of validators, the value to be tested must be passed as the first argument. Depending on the validator, it is also allowed to pass an object with a constraint property, which represents a comparison that must be done by the validator. Another possible property for the second argument is the reference, which serves as a basis for comparisons, when used. These last two properties are better explained in the documentation for validators and constraints.
Methods
Method | Description |
---|---|
jSuites.validations.number(value, options); | Check if the value is a number. |
jSuites.validations.date(value, options); | Check if the value is a date. |
jSuites.validations.text(value, options); | Check if the value is a text. |
jSuites.validations.itemList(value, options); | Check if the value is part of the list. This method does not use any constraint and in its reference property, an array with the allowed values must be passed. |
jSuites.validations(value, options); | The name of one of the previous functions must be passed in options as the 'type' property. The function with that name will be called with all arguments passed to the 'validations' function. |
Constraints
Constraint | Allowed methods | Description |
---|---|---|
= | date | number |
> | date | number |
>= | date | number |
< | date | number |
<= | date | number |
between | date | number |
out | date | number |
contains | text | The validator also checks if the reference value is present within the value. |
not contain | text | The validator also checks if the reference value is not present within the value. |
text | The validator also checks if the value is a valid email. |