# jQuery, RoR, validating fields against a database

**URL:** <https://www.ruby-forum.com/t/jquery-ror-validating-fields-against-a-database/182641>\
**Category:** Rails\
**Created:** [January 31, 2010, 8:48pm UTC](https://www.ruby-forum.com/t/jquery-ror-validating-fields-against-a-database/182641 "2010-01-31T20:48:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 31, 2010, 8:48pm UTC](https://www.ruby-forum.com/t/jquery-ror-validating-fields-against-a-database/182641/1 "2010-01-31T20:48:23Z")

</div>

I have a list of fields in a form that need to be validated against a  
database.

I would prefer to have the validation occur every time the field loses  
focus (onblur event) rather than when the user does a submit.

Does anyone know of an RoR example of how to do that?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 31, 2010, 8:55pm UTC](https://www.ruby-forum.com/t/jquery-ror-validating-fields-against-a-database/182641/2 "2010-01-31T20:55:29Z")

</div>

On Jan 31, 2010, at 11:48 AM, Ralph S. wrote:

> I have a list of fields in a form that need to be validated against a  
> database.
> 
> I would prefer to have the validation occur every time the field loses  
> focus (onblur event) rather than when the user does a submit.
> 
> Does anyone know of an RoR example of how to do that?

jQuery validate?

[http://docs.jquery.com/Plugins/Validation](http://docs.jquery.com/Plugins/Validation)

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 31, 2010, 9:07pm UTC](https://www.ruby-forum.com/t/jquery-ror-validating-fields-against-a-database/182641/3 "2010-01-31T21:07:57Z")

</div>

Steve R. wrote:

> jQuery validate?
> 
> [http://docs.jquery.com/Plugins/Validation](http://docs.jquery.com/Plugins/Validation)

Oh wow! … Thanks!!!

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 31, 2010, 10:04pm UTC](https://www.ruby-forum.com/t/jquery-ror-validating-fields-against-a-database/182641/4 "2010-01-31T22:04:01Z")

</div>

Ralph S. wrote:

> I have a list of fields in a form that need to be validated against a  
> database.
> 
> I would prefer to have the validation occur every time the field loses  
> focus (onblur event) rather than when the user does a submit.
> 
> Does anyone know of an RoR example of how to do that?

Remember not to rely on client-side validation. It’s very useful, but  
it’s easy to turn off JS and bypass it.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 31, 2010, 10:04pm UTC](https://www.ruby-forum.com/t/jquery-ror-validating-fields-against-a-database/182641/5 "2010-01-31T22:04:14Z")

</div>

Steve R. wrote:

> On Jan 31, 2010, at 11:48 AM, Ralph S. wrote:
> 
> > I have a list of fields in a form that need to be validated against a  
> > database.
> > 
> > I would prefer to have the validation occur every time the field loses  
> > focus (onblur event) rather than when the user does a submit.
> > 
> > Does anyone know of an RoR example of how to do that?
> 
> jQuery validate?
> 
> [http://docs.jquery.com/Plugins/Validation](http://docs.jquery.com/Plugins/Validation)

I think the jQuery validate function I want is “remote”.

Is there any way for that function to call a RoR action in a model or  
controller rather than more php or javascript?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 31, 2010, 10:41pm UTC](https://www.ruby-forum.com/t/jquery-ror-validating-fields-against-a-database/182641/6 "2010-01-31T22:41:59Z")

</div>

On Jan 31, 2010, at 1:04 PM, Ralph S. wrote:

> > > Does anyone know of an RoR example of how to do that?
> > 
> > jQuery validate?
> > 
> > [http://docs.jquery.com/Plugins/Validation](http://docs.jquery.com/Plugins/Validation)
> 
> I think the jQuery validate function I want is “remote”.
> 
> Is there any way for that function to call a RoR action in a model or  
> controller rather than more php or javascript?

It’s easy. I don’t have any code close at hand, but it’s pretty  
straightforward – stick the validation URL in an attribute you can  
access then use that. Just remember to have your authenticity  
certificate as part of the post data so Rails doesn’t suspect a forgery.

Marnen’s right, you shouldn’t rely completely on client-side validation,  
but I typically do as much validation as possible on the client to avoid  
beating up the server and/or spoiling the client experience, then  
validate everything completely on the server to prevent attacks or  
mis-entries on the part of people who have disabled js.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 31, 2010, 11:03pm UTC](https://www.ruby-forum.com/t/jquery-ror-validating-fields-against-a-database/182641/7 "2010-01-31T23:03:04Z")

</div>

On Jan 31, 2010, at 1:04 PM, Ralph S. wrote:

> > > Does anyone know of an RoR example of how to do that?
> > 
> > jQuery validate?
> > 
> > [http://docs.jquery.com/Plugins/Validation](http://docs.jquery.com/Plugins/Validation)
> 
> I think the jQuery validate function I want is “remote”.
> 
> Is there any way for that function to call a RoR action in a model or  
> controller rather than more php or javascript?

Here’s some old code I pulled off the shelf…uses jq validate

var validator = $(“#new\_user”).validate({  
rules: {  
“user[login]”: {  
required: true,  
minlength: 3,  
remote: “/users/login\_taken”  
}  
}  
});

Pardon the abysmal formatting.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 31, 2010, 10:06pm UTC](https://www.ruby-forum.com/t/jquery-ror-validating-fields-against-a-database/182641/8 "2010-01-31T22:06:04Z")

</div>

Ralph S. wrote:

> Steve R. wrote:
> 
> > On Jan 31, 2010, at 11:48 AM, Ralph S. wrote:
> > 
> > > I have a list of fields in a form that need to be validated against a  
> > > database.
> > > 
> > > I would prefer to have the validation occur every time the field loses  
> > > focus (onblur event) rather than when the user does a submit.
> > > 
> > > Does anyone know of an RoR example of how to do that?
> > 
> > jQuery validate?
> > 
> > [http://docs.jquery.com/Plugins/Validation](http://docs.jquery.com/Plugins/Validation)
> 
> I think the jQuery validate function I want is “remote”.
> 
> Is there any way for that function to call a RoR action in a model or  
> controller rather than more php or javascript?

If you can call PHP, you can call Rails. The client-side JS knows  
nothing about the server-side scripting language.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
