Javascript library that offers inline validation for web forms.
<script src="tick_of_truth.js"></script>
To build a test case, you need a selector validate("#password")
, validation rules .isMinLength(8).isMaxLength(24)
. To execute the test case, you then need a call to evaluate .result()
.
var testCase = validate("#password")
.isMinLength(8)
.isMaxLength(24);
testCase.result();
-> true/false
Multiple fields can also be chained together into a single rule.
var testCase = validate("#selector1")
.isEmail()
.validate("#selector2")
.isPhone();
Chained at the end of a test case to evaluate whether the validation has been met or not.
Returns true (pass) or false (fail) depending on the result of the evaluation.
var test = validate("#emailAddress")
.isEmail();
test.result();
-> true/false
Used to confirm that the test case has passed validation.
Returns true (pass) or false (fail) depending on the result of the evaluation.
var test = validate("#emailAddress")
.isEmail();
test.pass();
-> true/false
Used to confirm that the test case has failed validation.
Returns true (fail) or false (pass) depending on the result of the evaluation.
var test = validate("#emailAddress")
.isEmail();
test.fail();
-> true/false
During evaluation, if a test case is not met, the library will look for the parent .form-group
and add an .error
class to it. This will add a red bar to the left of the .form-group
and highlight the input
in red.
If a .label-error
, .legend-error
or .error-message
exists within the .form-group
, it will be shown automatically, presenting the error to the user.
If a .form-group
has more than one .label-error
, .legend-error
or .error-message
, you may want to show a specific error for different test cases. Using the error(String: selector)
function, you can specify which selector should be made visible.
validate("#email")
.isNotEmpty()
.error(".empty-email")
.result();
validate("#email")
.isEmail()
.error(".valid-email")
.result();
If you use an .error-summary
at the top of the page, the .error-summary
will automatically appear if any error is triggered within the page. By using an error with the same class in both the .error-summary
and .form-group
, both errors will show. for example,
<div class="error-summary">
<div class="error-title">There was a problem</div>
<ul class="error-list">
<li class="email-invalid"><a href="#">Provide a valid email</a></li>
</ul>
</div>
<div class="form-group">
<label for="email">
<div class="label-title">Email address</div>
<div class="label-error email-invalid">Provide a valid email</div>
<input type="text" id="email" class="form-control form-control-1-2" />
</label>
</div>
...
<script>
function submit() {
validate("#email")
.isEmail()
.error('.email-invalid')
.result();
}
</script>
If errors need to be reset and hidden for resubmission of a form, use the resetErrors()
function. This will hide every .error-summary
, .label-error
, .legend-error
and .error-message
.
validate.prototype.resetErrors();
Returns true if the value of the selector is the same as the supplied value.
validate("#name")
.is("Liam");
Returns true if the value of the selector contains only letters and numbers.
validate("#field")
.isAlphaNumeric();
Returns true if the selector has at least one checked radio or checkbox.
validate("input[name=radio-button-group]")
.isChecked();
Returns true if the value of the selector matches is a correctly formatted driving licence number.
validate("#driving-licence)
.isDrivingLicence();
Returns true if the value of the selector is a correctly formatted email address.
validate("#email")
.isEmail();
Returns true if the value of the selector is empty.
validate("#field")
.isEmpty();
Returns true if the value of the selector is greater than the supplied limit.
validate("#number")
.isGreaterThan(0);
Returns true if the value of the selector is greater than or equal to the supplied limit.
validate("#number")
.isGreaterThanOrEqual(0);
Returns true if the value of the selector is an integer (whole number).
validate("#number")
.isInteger();
Returns true if the length of the selector value matches the supplied length.
validate("#code")
.isLength(4);
Returns true if the value of the selector is less than the supplied limit.
validate("#number")
.isLessThan(100);
Returns true if the value of the selector is less than or equal to the supplied limit.
validate("#number")
.isLessThanOrEqual(100);
Returns true if the length of the selector value is greater than the supplied length.
validate("#password)
.minLength(2);
Returns true if the length of the selector value is less than the supplied length.
validate("#password")
.maxLength(24);
Returns true if the value of the selector is not the same as the supplied value.
validate("#phone")
.isNot("999");
Returns true if the value of the selector is not empty.
validate("#some-field")
.isNotEmpty();
Returns true if the value of the selector does not match the supplied expression.
validate("#value")
.isNotRegEx(/^[0-9]{6}$/);
Returns true if the value of the selector is any kind of number.
validate("#price")
.isNumber();
Returns true if the value of the selector is a correctly formatted phone number.
validate("#phone-number")
.isPhone();
Returns true if the value of the selector is a correctly formatted postcode.
validate("#postode")
.isPostcode();
Returns true if the value of the selector matches the supplied expression.
validate("#six-digits")
.isRegEx(/^[0-9]{6}$/);
Returns true if the value of the primary selector matches the value of the secondary selector.
validate("#email")
.isSameAs("#email-confirmation");
Returns true if the value of the selector is a correctly formatted Vehicle Identification Number.
validate("#vin")
.isVIN();