Sometime last year I worked on a simple JavaScript library for validating HTML forms. Recently I had to use it again and though maybe it could be useful to others and therefore decided to post it here on my blog.
The validation library is quite simple and can easily be extended to support customised validation routines.
The core library consists of two classes; FormValidator and ValidateItem (See class diagram)
The FormValidator class is the main class which controls which form fields to
validate and how to present the error message upon failure of validation.
The ValidationItem is an interface specifying how a certain type of validation should be implemented. The only requirement to implement the ValidationItem interface is to include a validate() function which must return a boolean value.
The validation library comes with a set of common ValidationItem classes for various type of validation such as e-mail addresses, URLs, IP addresses, and numbers:
- ValidationItemEmpty
Validates that a form field should not be empty
- ValidationItemLessThan
Validates that a form field should be less than a given number.
- ValidationItemGreaterThan
Validates that a form field should be greater than a given number.
- ValidationItemBetween
Validates that a form field should be in between to given numbers.
- ValidationItemMinimumLength
Validates that the length of a form field has a minimum character length.
- ValidationItemMaximumLength
Validates that the length of a form field has a maximum character length.
- ValidationItemBetweenLength
Validates that the length of a form field is in between a minimum and maximum character length.
- ValidationItemIPv4
Validates that a form field contains a valid IP address (IPv4).
- ValidationItemEmailAddress
Validates that a form field contains a valid e-mail address.
- ValidationItemURL
Validates that a form field contains a valid HTTP/HTTPS uniform resource location.
- ValidationItemInteger
Validates that a form field contains an integer.
- ValidationItemPositiveInteger
Validates that a form field contains a positive integer.
Alright, so now you know a bit about the structure of the library. Let’s have a look at how to use the library in your own solutions.
How to use the library:
- Place the library (FormValidator.js and ValidateItem-Common.js in the scripts directory)
- Include the library in the the page that should be validated.
- The library then needs to be instantiated
- The error display can then be configured to include the text to display above the error messages.
- Create the HTML form that the user should fill out.
- Add each of the form fields that should be validated to the instantiated validation object.
- Add the CSS classes used in the validation items to the style sheet
- Add a hook to the callback function that instantiates the validation class
- In the end of the callback function that instantiates the validation class return the validate() function of the instantiated object.
Full example:
<html>
<head>
<title>Login</title>
<script language="Javascript" src="FormValidator.js" mce_src="FormValidator.js"
type="text/javascript"></script>
<script language="Javascript" src="ValidationItem-Common.js" mce_src="ValidationItem-Common.js"
type="text/javascript"></script>
<script language="Javascript" type="text/javascript">
// Create instance of the form validator
var validator = new FormValidator();
validator.setErrorIntroText("Please correct the following:\n\n");
// Create validation items
var txtUsername = new ValidationItemEmailAddress("txtUsername",
"valid", "invalid", "Username is not a valid e-mail address");
var txtPassword = new ValidationItemEmpty("txtPassword", "valid",
"invalid", "Password cannot be empty");
// Add validation items to validator
validator.addValidationItem(txtUsername);
validator.addValidationItem(txtPassword);
function validateForm() {
return validator.validate();
}
</script>
<style type="">
.invalid {
background: red;
color: white;
}
.valid {
background: white;
color: black;
}
</style>
</head>
<body>
<h1>Login</h1>
<form onsubmit="return validateForm()" action="">
<table>
<tr>
<td>Username:</td>
<td>
<input type="text" name="txtUsername" id="txtUsername"
value="" />
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<input type="password" name="txtPassword"
id="txtPassword" value="" />
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login" /></td>
</tr>
</table>
</form>
</body>
</html>
There you have it. A pretty simple validation library.
A link to the class library will be posted soon.