moving form validation to client-side

43 views
Skip to first unread message

Dev Guy

unread,
Mar 4, 2012, 5:08:34 PM3/4/12
to [Rails]
Hi I know with javascript we can validate the input of a form on the
client, is there an easy way to specify this in rails? I am thinking
the javascript code getting generated using some sort of DSL?

--
Kind Regards,
Rajinder Yadav

SafetyNet Test Driven Development
http://safetynet.devmentor.org

Dev Guy

unread,
Mar 4, 2012, 6:07:12 PM3/4/12
to [Rails]
On Sun, Mar 4, 2012 at 5:08 PM, Dev Guy <devg...@gmail.com> wrote:
> Hi I know with javascript we can validate the input of a form on the
> client, is there an easy way to specify this in rails? I am thinking
> the javascript code getting generated using some sort of DSL?
>

While looking around I did mange to come across these two, however I
like the jQuery solution better

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

http://livevalidation.com/examples

Dev Guy

unread,
Mar 5, 2012, 12:36:48 AM3/5/12
to [Rails]
On Sun, Mar 4, 2012 at 6:07 PM, Dev Guy <devg...@gmail.com> wrote:
> On Sun, Mar 4, 2012 at 5:08 PM, Dev Guy <devg...@gmail.com> wrote:
>> Hi I know with javascript we can validate the input of a form on the
>> client, is there an easy way to specify this in rails? I am thinking
>> the javascript code getting generated using some sort of DSL?
>>
>
> While looking around I did mange to come across these two, however I
> like the jQuery solution better
>
> http://bassistance.de/jquery-plugins/jquery-plugin-validation/
>
> http://livevalidation.com/examples
>

Has anyone got the jquery validator working? I find it's buggy, when
validating password, it's very obtrusive! I get an error as soon as I
type, then it deletes what's been typed in the password field, which
for me if 1 character!

Mohamad El-Husseini

unread,
Mar 5, 2012, 7:14:40 AM3/5/12
to rubyonra...@googlegroups.com

Dev Guy

unread,
Mar 5, 2012, 1:59:16 PM3/5/12
to rubyonra...@googlegroups.com
On Mon, Mar 5, 2012 at 7:14 AM, Mohamad El-Husseini
<hussei...@gmail.com> wrote:
> Have you seen
> this? http://railscasts.com/episodes/263-client-side-validations
>
>
> On Sunday, March 4, 2012 7:08:34 PM UTC-3, Rajinder Yadav wrote:
>>
>> Hi I know with javascript we can validate the input of a form on the
>> client, is there an easy way to specify this in rails? I am thinking
>> the javascript code getting generated using some sort of DSL?
>>

Hi Mohamad, thanks I will take a look at this!

vishal singh

unread,
Mar 6, 2012, 10:21:37 AM3/6/12
to rubyonra...@googlegroups.com
jQuery(function(){
jQuery('.ValidField').each(function(){
jQuery(this).validate({
expression: "if (VAL) return true; else return false;",
message: "Please enter the Required field"
});
});
jQuery('.ValidInteger').each(function(){
jQuery(this).validate({
expression: "if (VAL.match(/^[0-9]*$/) && VAL) return true; else return false;",
message: "Please enter a valid numeric value"
});
});
jQuery(".ValidDropdown").each(function(){
jQuery(this).validate({
expression: "if (VAL != '') return true; else return false;",
message: "Please make a selection"
});
});
//put * on valid fields
//jQuery('.ValidField').prev().prev().after('*').css('color','#FF0000');
//prev prev => <label>
jQuery('.ValidField, .ValidInteger, .RequiredField, .ValidDropdown,.ValidEmail').prev().prev().after('*');
// for valid emails fields
jQuery(".ValidEmail").each(function(){
jQuery(this).validate({
expression: "if (VAL.match(/^[^\\W][a-zA-Z0-9\\_\\-\\.]+([a-zA-Z0-9\\_\\-\\.]+)*\\@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*\\.[a-zA-Z]{2,4}$/)) return true; else return false;",
message: "Please enter a valid Email ID"
});
});
//for valid date format
jQuery(".ValidDate").each(function(){
jQuery(this).validate({
expression: "if (!isValidDate(parseInt(VAL.split('-')[2]), parseInt(VAL.split('-')[0]), parseInt(VAL.split('-')[1]))) return false; else return true;",
message: "Please enter a valid Date"
});
});
});


add this jquery in application.js in app/assests/javascript/application.js and add javascript.validation.js in javascript which i am sending to as an attachment.hope this will working for you.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


jquery.validate.js
Reply all
Reply to author
Forward
0 new messages