Validation Types
How many different ways or types of validation can we carry out.- → Scenarios: When to Validation
- → Required Inputs
- → Min-Length Validation
- → Data Type Validation
- → Custom Expression Validation
Custom Expression Validation
Whether a field is required or not, a field can be checked against a number of custom expressions. If a field is not required, the custom expression validation is checked only if the input has value.The input attribute data-val-checkAgainst is used to specify a number pre-defined type of expressions.
You can test for the following types of custom expressions:
Equal To | Another Input | An Array | Numeric Operators | String Operators | Date Operators | RegEx Expression
String Operators
Supply a string operator expression. The input field's value must match the expression. Expressions start with a prefix (exp: ), then a space and then the operator such as !=, ==, etc. Expressions are case sensitive except for the operators that specify otherwise.Follow are the numeric operator data-val-checkAgainst options:
'exp: == S' - Checks if the value is equal to the string (S)
'exp: != S' - Checks if the value is NOT equal to the string (S)
'exp: ^= S' - Checks if the value starts with the string (S)
'exp: ^i= S' - Checks if the value starts with the string (S) - not case sensitive
'exp: $= S' - Checks if the value ends with the string (S)
'exp: $i= S' - Checks if the value ends with the string (S) - not case sensitive
'exp: *= S' - Checks if the value contains the string (S)
'exp: *i= S' - Checks if the value contains the string (S) - not case sensitive
Example:
<input type="text" name="string1" data-val-required="1" data-val-checkAgainst="exp: == Hello" /> <input type="text" name="string2" data-val-required="1" data-val-checkAgainst="exp: != Hello" /> <input type="text" name="string3" data-val-required="1" data-val-checkAgainst="exp: ^= He" /> <input type="text" name="string4" data-val-required="1" data-val-checkAgainst="exp: ^i= He" /> <input type="text" name="string5" data-val-required="1" data-val-checkAgainst="exp: $= lo" /> <input type="text" name="string6" data-val-required="1" data-val-checkAgainst="exp: $i= lo" /> <input type="text" name="string7" data-val-required="1" data-val-checkAgainst="exp: *= ll" /> <input type="text" name="string8" data-val-required="1" data-val-checkAgainst="exp: *i= ll" />