Giter Club home page Giter Club logo

knockout-simple-validation's Introduction

#knockout-simple-validation (ksv)

This is a simple JavaScript library to validate knockout (http://knockoutjs.com) models.

My english is terrible, therefore, any revision will be welcome

##Dependencies

- jquery-1.4.1 or higher
- knockout-2.0 or higher
- momentjs-2.8 or higher

All dependencies are available in directory lib/vendor.

##Built-in validation rules

  • required: Makes the observable property be required
  • email: Makes the observable property accept only email
  • url: Makes the observable property accept only url
  • nativeDate: Makes the observable property accept only a JavaScript Date object
  • date: Makes the observable property accept only a date as string in specific format
  • number: Makes the observable property accept only number objects
  • interger: Makes the observable property accept only number objects without decimal values.
  • max: Makes the observable property accept only values less than or equals to the specified value
  • min: Makes the observable property accept only values great than or equals to the specified value
  • maxlength: Makes the observable property accept only string values with length less than or equals to specified value
  • minlength: Makes the observable property accept only string values with length great than or equals to specified value
  • rangelength: Makes the observable property accept only string values with length in specified range
  • equalsTo: Makes the observalbe property accept only values equals to other property
  • remote: Makes the observable property to request a resource to check if value is valid

##Quickstart To use ksv in your project you have include the ksv.js script and its dependencies in your page.

The two examples below demonstrate a basic use of the library.

<html>
  <head>
    <title>knockout simple validation (ksv) - simple example</title>
  </head>
  <body>
    <form>
       <labe for="name">Name:</label><br/>
       <input type="text" id="name" data-bind="value: name"><br/>
       <span data-bind="text: name.error"></span><br/><br/>
       <labe for="email">E-mail:</label><br/>
       <input type="text" id="email" data-bind="value: email"><br/>
       <span data-bind="text: email.error"></span><br/>
    </form>
    <script src="assets/js/vendor/jquery-2.1.1.min.js"></script>
    <script src="assets/js/vendor/knockout-3.2.0.min.js"></script>
    <script src="assets/js/vendor/moment-2.8.3.min.js"></script>
    <script src="assets/js/vendor/ksv.min.js"></script>
    <script>
       function PersonModel() {
         this.name = ko.observable().extend({validator: {required: true}});
         this.email = ko.observable().extend({validator: {required: true, email: true}});
         this.age = ko.observable().extend({validator: {number: true, min: 18, max: 90}});
       }
       var personModel = new PersonModel();
       ko.applyBindings(personModel);
    </script>
  </body>
</html>
<html>
  <head>
    <title>knockout simple validation (ksv)</title>
  </head>
  <body>
    <form>
       <labe for="name">Name:</label><br/>
       <input type="text" id="name" data-bind="value: name"><br/>
       <span data-bind="text: name.error"></span><br/><br/>
       <labe for="email">E-mail:</label><br/>
       <input type="text" id="email" data-bind="value: email"><br/>
       <span data-bind="text: email.error"></span><br/>
       <input type="submit" value="Submit" data-bind="enable: isValid">
    </form>
    <script src="assets/js/vendor/jquery-2.1.1.min.js"></script>
    <script src="assets/js/vendor/knockout-3.2.0.min.js"></script>
    <script src="assets/js/vendor/moment-2.8.3.min.js"></script>
    <script src="assets/js/vendor/ksv.min.js"></script>
    <script>
       function PersonModel() {
         var ksvForThis = ksv.notification.makeObservable(this);
         this.name = ksvForThis.validatable('name', {required: true});
         this.email = ksvForThis.validatable('email', {required: true, email: true});
       }
       var personModel = new PersonModel();
       ko.applyBindings(personModel);
    </script>
  </body>
</html>

knockout-simple-validation's People

Contributors

francofabio 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.