Prototype Validation using Ajax

5 June 2007 22:34

I’ve been using the excellent Prototype validation over at <a href=”http://tetlaw.id.au/view/javascript/really-easy-field-validation”>dexagogo</a> but recently wanted to have a a feature so as when you type a username in a sign up form it fires a real time lookup to check the username availability. After a bit of Googling and some experimentation I came up with the following. Hopefully it’ll save you some time. In this case the lookup is done via Coldfusion which encodes and returns JSON.

The form bit is pretty standard.

<div class="form-row">
<div class="field-label"><label for="field1">Username</label>:</div>
<div class="field-widget"><input name="field1" id="field1" class="text required validate-username" title="Enter yourusername." /></div>
</div>

The Javascript bit included on this page in an external JS file.

Validation.addAllThese([
['validate-username','Sorry, that username is already taken.',function(v,elm) {
if(elm._ajaxValidating && elm._hasAjaxValidateResult) {
elm._ajaxValidating = false;
elm._hasAjaxValidateResult = false;
return elm._ajaxValidateResult;
}
var sendRequest = function() {
new Ajax.Request('/path/to/check/file?username='+v+'&r='+Math.random(),{
onSuccess : function(response) {
elm._ajaxValidateResult = eval(response.responseText);
elm._hasAjaxValidateResult = true;
Validation.test('validate-username',elm);
}
});
elm._ajaxValidating = true;
return true;
}
return elm._ajaxValidating || Validation.get('IsEmpty').test(v) || sendRequest();
}]
]);

The Javascript bit in the page.

<script language="javascript" type="text/javascript">
var valid_register = new Validation('register', {immediate:true,stopOnFirst:true,onSubmit:false});
</script>

The form is submitted with an Event.observe on the submit button I have as well.

And so concludes my first geekish post for a while, it feels strangely good!

Filed: Technology // Tagged: ,

You can follow any responses to this entry through the RSS 2.0 feed.

You can leave a comment or leave a trackback from your own site.

2 Responses to “Prototype Validation using Ajax”

  1. jarrold on July 24th, 2009 at 7:35 am

    Thanks for this. Just what I was looking for.

  2. Thunderbird on August 11th, 2009 at 5:25 am

    Hello Ian,
    Very good post, this is exactly what I am looking for, but for some strange reason it’s not working. Wondering if you can have a demo of this somewhere? I am not even getting any error message so I can debug it. Waiting for your reply.

    Cheers
    Thunderbird

Leave a Reply

 
What's ianteresting?   •   Twitter   •   About   •   Contact
©2010 Ian Winter. All Rights Reserved.   •   Powered by WordPress   •   Hosted at Memset