Giter Site home page Giter Site logo

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

View Code? Open in Web Editor NEW

This project forked from ddarren/jquery-live-form-validation-for-twitter-bootstrap

1.0 2.0 0.0 76 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 mikewaters avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.