Big Al\’s Blog

Software Engineering, Enterprise Computing and The Digital Divide

Archive for the ‘Web Programming’ Category

Moved the JavaScript Validation Library to Google Code Repository

Posted by Big Al on August 8, 2006

Since WordPress don’t actually allow file storing on the free blog hosting I decided to set-up a project on the Google Code Repository: http://code.google.com/p/simple-jsvalidator/

You can get the code through the Subversion WebDAV front-end: http://simple-jsvalidator.googlecode.com/svn/tags/1.0.0/  or if you want to commit changes to the code you can e-mail me and I’ll add you as a project member (allowing you to commit your changes).

Posted in Software Engineering, Web Programming | Comments Off on Moved the JavaScript Validation Library to Google Code Repository

Finally posting the JavaScript validation classes

Posted by Big Al on July 18, 2006

I wasn’t able to post the JavaScript validation classes that I mentioned previously when I was blogging on BlogSpot (http://www.blogger.com). For some reason they don’t allow file uploads. Anyways, now that I’m back on WordPress, here are the validation classes (refer to previous blog entry for usage)

JavaScript Validation Library: https://lykke.wordpress.com/wp-content/uploads/2006/07/js-validation.jpg

Posted in Web Programming | 2 Comments »

JavaScript class for validation

Posted by Big Al on May 28, 2006

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:

  1. Place the library (FormValidator.js and ValidateItem-Common.js in the scripts directory)
  2. Include the library in the the page that should be validated.
  3. The library then needs to be instantiated
  4. The error display can then be configured to include the text to display above the error messages.
  5. Create the HTML form that the user should fill out.
  6. Add each of the form fields that should be validated to the instantiated validation object.
  7. Add the CSS classes used in the validation items to the style sheet
  8. Add a hook to the callback function that instantiates the validation class
  9. 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.

Posted in Web Programming | 2 Comments »