Valid8
Valid8 solves both simple and complex validation scenarios. Everything from a basic required field to regular expressions, ajax requests and arbitrary javascript functions. There is nothing you can’t validate using Valid8.
Valid8 features support for three types of validation techniques; Regular expressions, custom javascript functions and Ajax requests. The three techniques can be combined in any way you want. For example, in a very extreme case, you can use two regular expressions, one javascript function and three ajax requests for validating a single input field.
- Regular expressions
- For determining whether a value matches a regular expresion. For example determining if a user name consists of only letters and numbers.
- Javascript functions
- Send one or multiple values to a custom function. For example checking if the ‘password’ and the ‘confirm password’ input field matches each other.
- Ajax requests
- Post one or multiple values to a external file. For example to see if a user name already exists in the database.
Examples and demos
Valid8 ranges from super simple ‘making an input field required’ to an near endless possibility of complexity. I think the best way for me to describe how Valid8s individual parts is by showing you a set of examples.
- How to make an input field required
- This example will show you how to make an input field required and show the default validation message ‘Required’ if the field is left empty.
- How to make an input field required and show a custom error message
- This example will show you how to make an input field required and show a custom validation message if the field is left empty.
- How to validate with a single regular expression
- This example will show you how to validate using a regular expression that dictates that the input field(s) only can contain letters and numbers.
- How to validate with multiple regular expressions
- This example will show you how to validate using two regular expressions. One that dictates that the input field is required and one that the input field only can contain letters and numbers. Valid8 will show a different validation message depending on which regular expression it violates first.
- How to validate with a javascript function
- This is example will show you how to compare the Password field with the Confirm Password field and make Confirm Passoword invalid if it doesn’t match the Password field.
- How to validate using an ajax request
- This example will show you how to make see if an email address already is in your database.
Example – How to make an input field required
Calling the valid8 method without any arguments will show the default validation error message ‘Required’ if the input field is left empty.
- $('#inputUsername').valid8();
Example – How to make an input field required and show a custom error message
Calling the valid8 method with a string as an argument will show the string as an a validation error message if the input field is left empty.
- $('#inputUsername').valid8('Username is required');
Example – How to validate with a single regular expression
The example below is using a regular expression that dictates that the input field(s) only can contain a-z, A-Z and 0-9. If the value contains for example the character ‘@’ the validation message ‘You can only use the letters A-Z and numbers’ will be shown.
- $('#inputUsername').valid8({
- 'regularExpressions': [
- { expression: /^[a-zA-Z0-9]+$/|>, errormessage: 'You can only use the letters A-Z and numbers'}
- ]
- });
Example – How to validate with multiple regular expressions
The example below is using two regular expressions that dictates that the input field(s) not can be empty and only can contain a-z, A-Z and 0-9. If the value contains for example the character ‘@’ the validation message ‘You can only use the letters A-Z and numbers’ will be shown. If the field is left completely empty it will show the validation message ‘Field is required’.
- $('#inputUsername').valid8({
- 'regularExpressions': [
- { expression: /^.+$/, errormessage: 'Username is required'},
- { expression: /^[a-zA-Z0-9]+$/|>, errormessage: 'You can only use the letters A-Z and numbers'}
- ]
- });
Example – How to validate with a javascript function
In this example validates the input field ‘#inputPasswordVerification’ and see if it matches the input field ‘#inputPassword’. If it doesn’t match the validation message ‘Passwords does not match’ will be shown.
- function doesPasswordFieldsMatch(values){
- if(values.password == values.verification)
- return {valid:true}
- else
- return {valid:false, message:'Passwords does not match'}
- }
- $('#inputPassword').valid8('Password is required');
- $('#inputConfirmPassword').valid8({
- 'jsFunctions': [
- { function: doesPasswordFieldsMatch, values: function(){
- return { password: $('#inputPassword').val(), verification: $('#inputConfirmPassword').val() }
- }
- }
- ]
- });
Example – How to validate with an ajax request
This example will post the value of the input field ‘#inputEmail’ to isEmailUnique.php.
- $('#inputEmail').valid8({
- 'regularExpressions': [
- { expression: /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/|>, errormessage: 'Email is not valid'},
- ],
- 'ajaxRequests': [
- { url: 'lib/isEmailUnique.php' }
- ]
- });
- $email = $_POST['value'];
- if(!isEmailUnique($email)){
- $json["valid"] = false;
- $json["message"] = 'Email is already in use';
- }
- else {
- $json["valid"] = true;
- }
- function isEmailUnique($email){
- // Database look-up should go here here...
- // But for the sake of this demo a random return will do
- return rand(0, 1);
- }
- print json_encode($json);
| Version | Changes |
|---|---|
| 1.3 | Valid8 now uses triggers instead of callback functions |
| 1.2.2 | Fixed a bug related to checkbox validation |
| 1.2.1 | Fixed a bug in IE7 |
| 1.2 | Faster, harder, stronger. |
| 1.1.2 | Fixed a spelling mistake |
| 1.1.1 | Fixed a bug where white-spaces would pass the default validation regexp. (Thanks to Srinivas Tamada for reporting the bug) |
| 1.1 | Changed the variable validationEvent to validationEvents. It can now take both strings (eg. 'keyup') and arrays with strings (eg: ['keyup','blur']). |
253 days ago
Read one article about glass; read this one about Corning, Corning’s Gorilla Glass, Apple and the future. http://t.co/xakeeQzU (via @VirUZI)
255 days ago
I just bought CHTEMELE; a boardgame about HTML5 for 2 – 5 players. http://t.co/kbpQLivu
257 days ago
A normal male ejaculation is equivalent of 16 terabyte of information. http://t.co/tkLndAwg
259 days ago
We are 18, we have no work, we don’t study, we are Anonymous. -We know. http://t.co/NHPa7gjR
259 days ago
We are 18, we have no work, we don’t study, we are Anonymous. -We know. http://t.co/NHPa7gjR
260 days ago
Anonymous plan to DDoS the Central Bank of Sweden’s website, in the middle of the Swedish night, to hurt our economy. Have fun, Goodnight ![]()
260 days ago
Book trailer for Instant: The story of Polaroid [the Apple of the 60s]. http://t.co/y4srSGmP