# validation problem

**URL:** <https://discuss.rubyonrails.org/t/validation-problem/45347>\
**Category:** rubyonrails-talk\
**Created:** [August 21, 2009, 5:43am UTC](https://discuss.rubyonrails.org/t/validation-problem/45347 "2009-08-21T05:43:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 21, 2009, 5:43am UTC](https://discuss.rubyonrails.org/t/validation-problem/45347/1 "2009-08-21T05:43:26Z")

</div>

Hello Guys i want to use external validation i mean javascript validation but whenever click on create i mean (add new record submit)that time my first text box is black then it give me alert but when i click on 'ok' it redirect to controller and give error not focus on same text box.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 21, 2009, 2:38pm UTC](https://discuss.rubyonrails.org/t/validation-problem/45347/2 "2009-08-21T14:38:13Z")

</div>

Wap Addon wrote:

> Hello Guys i want to use external validation i mean javascript validation but whenever click on create i mean (add new record submit)that time my first text box is black then it give me alert but when i click on 'ok' it redirect to controller and give error not focus on same text box.

Whoa, take a step back here and slow down so that we can understand you. That's one of the longest run-on sentences I've seen in my life.

You want to use javascript validation because you are using something like a form\_tag correct? Which tag are you using to start with?

Start with providing the basics of what you have, show some initial code for the piece you are having issues with.

You do not have to use javascript validation on external pieces. I have created custom validation methods for pieces like form\_tag, etc.

The larger question is you mentioned a create or a submit to add a record, which tells me that you are adding it into a database. If that is the case then your validations should be performed in the model, not with javascript.

---

<div class="post-metadata">

**Author:** ![Jaap\_Haagmans](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@Jaap\_Haagmans](https://discuss.rubyonrails.org/u/Jaap_Haagmans)\
**Post date:** [August 22, 2009, 7:57am UTC](https://discuss.rubyonrails.org/t/validation-problem/45347/3 "2009-08-22T07:57:45Z")

</div>

Why are you doing this in javascript when you can do this beautifully in your controller? Create the appropriate validations in your model and then in your controller check whether saving is possible. If it is, save it and redirect or whatever you want it to do. If not, catch the error and show it.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 22, 2009, 9:43am UTC](https://discuss.rubyonrails.org/t/validation-problem/45347/4 "2009-08-22T09:43:39Z")

</div>

jhaagmans wrote:

> Why are you doing this in javascript when you can do this beautifully in your controller? Create the appropriate validations in your model and then in your controller check whether saving is possible. If it is, save it and redirect or whatever you want it to do. If not, catch the error and show it.

ok dear but this is server side validation. i want to use client side validate.

server side take more time execution so that client side validation more fast. so that i want to use external js validation.

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 22, 2009, 11:22am UTC](https://discuss.rubyonrails.org/t/validation-problem/45347/5 "2009-08-22T11:22:09Z")

</div>

> jhaagmans wrote: \> Why are you doing this in javascript when you can do this beautifully \> in your controller? Create the appropriate validations in your model \> and then in your controller check whether saving is possible. If it \> is, save it and redirect or whatever you want it to do. If not, catch \> the error and show it.
> 
> ok dear but this is server side validation. i want to use client side validate.
> 
> server side take more time execution so that client side validation more fast. so that i want to use external js validation.

I hope you are also doing server side validation. This is better done from the form's onsubmit, and don't forget to return the value of your validation function (don't just call it as you are doing now)

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 22, 2009, 2:03pm UTC](https://discuss.rubyonrails.org/t/validation-problem/45347/6 "2009-08-22T14:03:58Z")

</div>

jhaagmans wrote:

> Why are you doing this in javascript when you can do this beautifully in your controller? Create the appropriate validations in your model and then in your controller check whether saving is possible. If it is, save it and redirect or whatever you want it to do. If not, catch the error and show it.

ok but as per my above validation code is not possible ?

---

<div class="post-metadata">

**Author:** ![Matt\_Jones](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@Matt\_Jones](https://discuss.rubyonrails.org/u/Matt_Jones)\
**Post date:** [August 22, 2009, 11:32pm UTC](https://discuss.rubyonrails.org/t/validation-problem/45347/7 "2009-08-22T23:32:53Z")

</div>

Don't think like this - as other posters have pointed out, you're going to have to do server-side validation anyways. Worrying about the execution time of something this small is exceptionally premature optimization.

In your example, the JS validation may work, but you're still going to have to validate server-side, as some users may have JS turned off.

--Matt Jones
