# Java script validations

**URL:** <https://www.ruby-forum.com/t/java-script-validations/78391>\
**Category:** Rails\
**Created:** [December 5, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/java-script-validations/78391 "2006-12-05T14:41:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 5, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/java-script-validations/78391/1 "2006-12-05T14:41:41Z")

</div>

Hi Everyone,  
Can anyone tell, how to validate for empty fields (or) certain  
patterns using java script (client side validations) in Ruby on Rails??  
I need to check whether the user has entered a “login id” and  
“password” before hitting the submit button.

Thanks in advance…  
With Regards,  
Vasanth

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 5, 2006, 7:46pm UTC](https://www.ruby-forum.com/t/java-script-validations/78391/2 "2006-12-05T19:46:21Z")

</div>

Does this degrade gracefully when users are not able to use Javascript?

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 5, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/java-script-validations/78391/3 "2006-12-05T18:46:14Z")

</div>

On Tuesday 05 December 2006 14:41, Vasanthakumar C. wrote:

> Hi Everyone,  
> Can anyone tell, how to validate for empty fields (or)  
> certain patterns using java script (client side validations) in Ruby  
> on Rails?? I need to check whether the user has entered a “login id”  
> and “password” before hitting the submit button.

Have a look at my client-side validation plugin

[http://www.agilewebdevelopment.com/plugins/client\_side\_validation](http://www.agilewebdevelopment.com/plugins/client_side_validation)

required:  
[http://www.agilewebdevelopment.com/plugins/validation\_reflection](http://www.agilewebdevelopment.com/plugins/validation_reflection)

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 5, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/java-script-validations/78391/4 "2006-12-05T19:48:21Z")

</div>

That was probably a dumb question… looks like it doesn’t affect  
normal  
flow.

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 5, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/java-script-validations/78391/5 "2006-12-05T20:48:29Z")

</div>

On 12/5/06, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Have a look at my client-side validation plugin
> 
> [http://www.agilewebdevelopment.com/plugins/client\_side\_validation](http://www.agilewebdevelopment.com/plugins/client_side_validation)
> 
> required:  
> [http://www.agilewebdevelopment.com/plugins/validation\_reflection](http://www.agilewebdevelopment.com/plugins/validation_reflection)

Cool michael, but where I put the code to validate? I used to write my  
own javascript validators and put them inside a in  
my layouts.

–  
Fernando L.

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 6, 2006, 12:11am UTC](https://www.ruby-forum.com/t/java-script-validations/78391/6 "2006-12-06T00:11:07Z")

</div>

On Tuesday 05 December 2006 20:39, Fernando L. wrote:

> in my layouts.  
> As long as you don’t need rather special validations there is no need to  
> write _any_ JavaScript code of your own. You automatically get  
> validations corresponding to

- validates\_presence\_of
- validates\_length\_of
- validates\_numericality\_of
- validates\_inclusion\_of
- validates\_format\_of - as far as regular expressions are matched the  
same in Ruby and JavaScript

If you do need more special validations, see “Custom Checking” in the  
README.

Judging from the questions I’ve got since I published the plugin, the  
trickier part is to tell the user there’s something wrong with their  
entered data. The validator runs all the time, not just when the user  
tries to submit a form. What it normally does is to  
add/remove “invalid” to/from the class attribute of input elements  
(text fields, selects, radio buttons). This change, of course is not  
visible to the user. You can make it visible with a short bit of CSS

.invalid {  
border: 1px solid #f00;  
}

Actually, I don’t recommend doing it exactly like this. But the  
specifics of error notification depend on the needs of your  
application. There’s also a way to supply a callback function to the  
validator that is called whenever the validation status of the form  
changes. For the details see the README and follow up with more  
specific questions.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 6, 2006, 6:56am UTC](https://www.ruby-forum.com/t/java-script-validations/78391/7 "2006-12-06T06:56:38Z")

</div>

Chris. Here’s what I did to get it working

1. In the plugin/client\_side\_validation/install.rb only include the two  
js files  
ActionView::Helpers::AssetTagHelper::register\_javascript\_include\_default(‘validator’)  
ActionView::Helpers::AssetTagHelper::register\_javascript\_include\_default(‘validators-en’)  
(This is included when defining \<%= javascript\_include\_tag :defaults %\>  
in the layout)

2. Add installForAllValidatedForms to the view

1. set the form class as ‘validated’ (I’m using edge rails)  
\<% form\_for(:article, :url =\> articles\_path, :html =\> {:class =\>  
“validated”}) do |f| %\>

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 6, 2006, 2:25am UTC](https://www.ruby-forum.com/t/java-script-validations/78391/8 "2006-12-06T02:25:25Z")

</div>

Ok…I’m trying to get this thing working, and I think I’m missing  
something fundamental. Do you have a simple example available to look  
at?

Here’s what I’ve done:

1. Install Plugins  
script/plugin install  
svn://rubyforge.org//var/svn/clientsidevali/client\_side\_validation/trunk  
script/plugin install  
svn://rubyforge.org//var/svn/valirefl/validation\_reflection/trunk

2. Create a model  
class Post \< ActiveRecord::Base  
validates\_length\_of :title, :within =\> 3…20  
end

3. Create a controller  
class PostsController \< ApplicationController  
def create  
Post.create(params[:post])  
end  
end

4. Create a view

 \<%= stylesheet\_link\_tag 'posts' %\> \<%= javascript\_include\_tag :defaults %\> \<%= form\_tag url = {:action =\> "create"}, options = {:class =\> 'validated'}%\> 

Title  
 \<%= text\_field 'post', 'title' %\>

 \<%= submit\_tag %\> \<%= end\_form\_tag %\>
1. Create /public/javascripts/application.js  
Form.Validator.installForAllValidatedForms();

2. Create a stylesheet  
.invalid {  
border: 1px solid #f00;  
}

3. Go to the browser, enter invalid input and expect something to  
happen.

---

<div class="post-metadata">

**Author:** ![vasan\_cec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/258eb7/32.png) [@vasan\_cec](https://www.ruby-forum.com/u/vasan_cec)\
**Post date:** [December 6, 2006, 10:56am UTC](https://www.ruby-forum.com/t/java-script-validations/78391/9 "2006-12-06T10:56:55Z")

</div>

On Wednesday 06 December 2006 06:56, Chris Douglas wrote:

> Chris. Here’s what I did to get it working
> 
> 1. In the plugin/client\_side\_validation/install.rb only include the  
> two js files  
> ActionView::Helpers::AssetTagHelper::register\_javascript\_include\_defa  
> ult(‘validator’)  
> ActionView::Helpers::AssetTagHelper::register\_javascript\_include\_defa  
> ult(‘validators-en’)  
> (This is included when defining \<%=  
> javascript\_include\_tag :defaults %\> in the layout)

Thanks for pointing this out. I have changed the code (and README) so  
that none of the locale-specific validations are included by default.  
Therefore, after updating the plugin, you have to include the necessary  
file explicitly in your layout. So

\<%= javascript\_include\_tag :defaults %\>

has to become

\<%= javascript\_include\_tag :defaults %\>  
\<%= javascript\_include\_tag ‘validators-en’ %\>

> 1. Add installForAllValidatedForms to the view

I recommend adding that line of JavaScript to  
public/javascripts/application.js as that file is included by default

> 1. set the form class as ‘validated’ (I’m using edge rails)  
> \<% form\_for(:article, :url =\> articles\_path, :html =\> {:class =\>  
> “validated”}) do |f| %\>

Yes.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)
