Thursday, 15 July 2010

client side validation - rails client_side_validations only on hit save -



client side validation - rails client_side_validations only on hit save -

i don't want clien-side-validations validate on blur inputs, when nail save.

is possible? how can this?

i have simple validations (empty, basically)... , i'm thinking write hand.. worth?

you can pure javascript or help of of jquery/prototype , similar frameworks.

here code tried jquery

<html> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <body> <form id="my_form" action="http://google.com?q=ruby" method="get"> <input type="text" name="first_name" placeholder="first name" /> <br/> <input type="text" name="last_name" placeholder="last name" /> <br/> <input type="text" name="city" placeholder="city" /> <br/> <input id="form_button" type="button" value="submit"/> </form> </body> </html> <script type="text/javascript"> $(document).ready(function() { $("#form_button").on('click', function() { var form_valid = true; $("#my_form > input[type='text']").each(function() { if(this.value == "") { var error_span_present = $(this).next("span").length != 0; if(!error_span_present) { $("<span style='color:red;'>" + $(this).attr("placeholder") + " cannot blank </span>").insertafter($(this)); } else { $(this).next("span").html($(this).attr("placeholder") + " cannot blank"); } form_valid = false; } else { if($(this).next("span").length != 0) { $(this).next("span").remove(); } } }); if(form_valid){ $("#my_form").submit(); } }); }); </script>

here jsfiddle demos code.

hope helps

ruby-on-rails client-side-validation

No comments:

Post a Comment