Giter VIP home page Giter VIP logo

ddarren / jquery-live-form-validation-for-twitter-bootstrap Goto Github PK

View Code? Open in Web Editor NEW
114.0 6.0 21.0 117 KB

This is an adaptation of the jQuery Live Form Validation found at http://www.geektantra.com/2009/09/jquery-live-form-validation/ for Twitter Boostrap. The modified script will allow for live form validation using the Twitter Bootstrap HTML pattern for forms.

JavaScript 100.00%

jquery-live-form-validation-for-twitter-bootstrap's Introduction

About

This is an adaptation of the jQuery Live Form Validation found here for Twitter Bootstrap. The modified script will allow for live form validation using the Twitter Bootstrap HTML pattern for forms.

The main script is jquery.validate.js. This has a validate method that you can call on any jQuery input object. The changes for Twitter Bootstrap are the names of couple of the parameters, and using a container div w/ class=control-group instead of the input field to highlight a validation error.

The jquery.validation.functions.js file has not been modified in anyway and simply provides helper methods for date and radio button validation.

The scripts currently utilize a default value suited for Twitter Bootstrap 2, but they should still be able work with earlier versions of Bootstrap by passing in the error_container_class parameter to override the default.

Instructions

  1. Copy the jquery.validate.js and jquery.validation.js files to your javascripts folder.

  2. Create your form using the formatted needed by Twitter Bootstrap. If you are using the twitter-bootstrap-rails gem, you can run "rails g bootstrap:themed [RESOURCE_NAME]" to create Twitter Bootstrap compatible scaffold views.

  3. Now add script that will validate your form inputs. This script will need to get executed after the page loads.

Example in CoffeeScript:

$(document).ready ->
  $("#car_make").validate
    expression: "if(VAL != '') return true; else return false;"
    message: "Make is required."

  $("#car_model").validate
    expression: "if(VAL != '') return true; else return false;"
    message: "Model is required."

  $("#car_year").validate
    expression: "if(VAL != '') return true; else return false;"
    message: "Year is required."

  $("#car_year").validate
    expression: "if(VAL.match(/^\\d\\d\\d\\d$/)) return true; else return false;"
    message: "Invalid format."

For above the validate call you may also pass in additional parameters to override the defaults: error_message_class, error_container_class, live (true/false). The first two parameters are CSS classes for errors and the live parameter is if you want validation when an input goes out of focus (true) or only when the submit button is clicked (false). The default for the live parameter is true.

Example Project

That's it! To view an example project head click here.

Special Note for Ruby on Rails

The scripts in this repository do not make use of Rails model validation helpers. If you wish to see a solution that utilizes Rails validations helpers to do live validation with Twitter Bootstrap, please checkout my blog post.


Original Script License: http://www.apache.org/licenses/LICENSE-2.0

Original Script Copyright: @author GeekTantra @date 20 September 2009

jquery-live-form-validation-for-twitter-bootstrap's People

Contributors

ddarren avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

jquery-live-form-validation-for-twitter-bootstrap's Issues

Hitting submit button with invalid fields doesn't trigger error messages on fields

When I have a validated field in a form and try to submit the form when that field is erroneous, the form doesn't submit (as expected) but the erroneous field remains the same, with no red border or error message (unexpected behavior). When I would edit the field and give it an invalid value, then click elsewhere, the field got a red border and my error message appeared beside it (as expected).

I think I fixed the problem in your validate_field method. I replaced this:

if (jQuery(id).next('.' + options['error_message_class']).length == 0) {
    jQuery(id).after('<span class="' + options['error_message_class'] + '">' + options['message'] + '</span>');
    jQuery(id).parents("div ." + options['error_container_class']).addClass("error")
}

With this:

var field = jQuery(id);
var errMsgHolder = field.next('.' + options['error_message_class']);
if (errMsgHolder.length < 1) {
    field.after('<span class="' + options['error_message_class']
            + '">' + options['message'] + '</span>');
} else {
    errMsgHolder.text(options['message']);
}
field.parents("." + options['error_container_class'])
    .addClass("error");

Error message breaks input-append

Validating error message in input group which has appended text goes between the <input> and the <span class="add-on">. It should be after <div class="input-append"> is closed.

Form with invalid field followed by valid file field does not mark any fields as erroneous on submit

I had a form with two fields, both validated. The first field required a value, and defaulted to having no value. The second field was type=file and did not require a value. Without touching either of the fields, I hit the submit button for the form. The first field was not marked as erroneous (unexpected behavior), nor was the second field (expected behavior). I think I fixed the problem. I changed this:

if (validate_field('#' + SelfID)) {

    jQuery('.' + options['error_message_class']).remove();
    jQuery('.' + options['error_container_class']).removeClass('error');

    return true;
}
else 
    return false;

To this:

var fieldSel = '#' + SelfID;
if (validate_field(fieldSel)) {
    jQuery(fieldSel + ' + .' + options['error_message_class']).remove();
    jQuery(fieldSel).closest('.' + options['error_container_class'])
            .removeClass('error');
    return true;
} else {
    return false;
}

You were removing all error messages and taking the 'error' class off of all control-group div's, instead of just those associated with the field that's valid.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.