views:

26

answers:

2

I basically want to prevent searches that are less than 3 characters long.

In Rails 2.x, I would just do this in an onClick event. I understand how to handle AJAX requests and responses in Rails 3 now, but wouldn't using the onClick event be considered obtrusive in Rails 3?

+1  A: 

Yup, onclick would indeed be considered obtrusive. You might want to use a third-party validation library in javascript. It could work something like this:

<form ... class="validated">
  <input type="text" name="name" data-validation="presence=true,format=/[a-zA-Z0-9\s]+/" />
  <input type="submit" value="Send" />
</form>

<script language="javascript">
  $('form.validated').live('submit', function() {
    var data = $(this).serializeArray();
    var errors = [];
    $(this).find('input').each(function() {
      var input = $(this);
      $.each($(this).attr('data-validation').split(','), function(validation) {
        var type, param = validation.split("=");
        if(type == "presence") {
          if(input.val() == "") errors += input.attr('name') + " is empty";
        } else if(type == "format") {
          ...
        }
    });
    return (errors.length == 0);
  });
</script>

(Just some rough code out of my head. Lots of caveats there.)

But instead of writing all that, perhaps you just want to use: http://github.com/jzaefferer/jquery-validation

balu
Would you please mark my answer as accepted? :)
balu
A: 
AKWF