Sep
6
Live Validation – Custom Validator


Posted: 6th September 2007
Tags: , , , ,
Posted in Javascript
Comments: 3 Comments »

Following my article earlier today on Live Validation, I have had a response from Alec regarding how to create a custom validator to use with his Live Validation framework.

Having asked if a custom Validator could be passed in the .Now() function Alec had the following to say:

In regards to passing a custom validation method into the .now method, it will not \’91create\’92 the validation method as such, but will accept one you have made, as long as your custom validation method takes a value as the first argument, and throws a Validate.Error exception when it fails (in order to pass on the failure message made easier by using Validate.fail), and true if it passes. For example:

  1. Validate.Example = function(value, paramsObj)\{
  2. // the following sets up default options and overides them with those passed in
  3. var params = Object.extend(\{
  4. failureMessage: Default failre message goes in here!
  5. \}, paramsObj || \{\});
  6. // here you would check the value you wish to validate (the one passed in), against some expected value
  7. // if it fails then use the Validate.fail method will throw an exception containing the failure message
  8. if(value != My expected value) Validate.fail(params.failureMessage);
  9. // if the script reaches this far then no exception has been thrown, so the validation passed
  10. return true;
  11. \}

Alec clarified: “…if you take a look at some of the included validation methods they contain calls to some of the other ones internally, as they will throw exceptions and halt proceedings if they fail, so act in much the same way as the simple one we have above. Validate.Email is a simple example of this and can be easily adapted to make a reusable postcode format validation for instance.”

Following Alec’s example above, you can easily then use the validator using the following:

  1. Validate.now(Validate.Example(), value_to_validate, \{options\})

Using a custom validator opens up many possibilities such as making an AJAX call to the server to check the uniqueness of a username or password.

For more information:

Live Validation

Documentation for Validate.now()

Comments below..

Advertisement

 




Comments

Note: You can leave a response, or trackback from your own site.

 
Comment:

You got better example ? Doesn’t seem to work with me. I just alway get true, whatever my input is.

 
 
Comment:

Hi Gaursson,

My advice is to contact Alec directly. Visit his site http://www.livevalidation.com

Jon

 
 
Pingback:

[...] Live Validation – Custom Validator Clockwork Objects is a small web development / design studio based in Truro, Cornwall, UK. We create web sites and web applications built with web standards, as well as Software Development. (tags: tts development prototype validation) [...]

 
Add a comment:

 


View Jon Baker's profile on LinkedIn


View blog authority