Knockout validation
Upcoming SlideShare
Loading in...5
×
 

Knockout validation

on

  • 4,807 views

 

Statistics

Views

Total Views
4,807
Views on SlideShare
4,807
Embed Views
0

Actions

Likes
2
Downloads
39
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Knockout validation Knockout validation Presentation Transcript

  • JASON
  • Knockout Validation  A KnockoutJS Plugin for model and property validation
  • Native Rules  Required  ko.observable().extend({ required: true });  Min  ko.observable().extend({ min: 2 });  Max:  ko.observable().extend({ max: 99 });  MinLength  ko.observable().extend({ minLength: 3 });  MaxLength:  ko.observable().extend({ maxLength: 12 });  Pattern:  ko.observable().extend({ pattern: '^[a-z0-9].$' });
  • Native Rules  Date  ko.observable().extend({ date: true });  Email:  ko.observable().extend({ email: true });  Equal:  ko.observable().extend({ equal: 2 });  Not Equal:  ko.observable().extend({ notEqual: 2 });  Number:  ko.observable().extend({ number: true });
  • Use var viewModel = { phone: ko.observable().extend({ required: true }); submit: function () { if (viewModel.errors().length == 0) { alert('Thank you.'); } else { viewModel.errors.showAllMessages(); } } }; viewModel.errors = ko.validation.group(viewModel); ko.validation.configure(); ko.applyBindingsWithValidation(viewModel);
  • Custom Validation Rules ko.validation.rules['mustEqual'] = { validator: function (val, otherVal) { return val === otherVal; }, message: 'The field must equal {0}' }; ko.validation.registerExtenders(); var myCustomObj = ko.observable().extend({ mustEqual: 5 });
  • Custom Message Template <script id="customMessageTemplate" type="text/html"> <em class="customMessage" data-bind='validationMessage: field'></em> </script> <div data-bind='validationOptions: { messageTemplate: "customMessageTemplate" }'> …… </div>
  • Configuration Option Default insertMessages true decorateElement false errorMessageClass ‘validationMessage’ errorElementClass ‘validationElement’ errorsAsTitle true parseInputAttributes false errorClass null ko.validation.configure({ insertMessages: true, errorsAsTitle:true, errorElementClass: “errorClass", errorMessageClass: “errorMessageClass", decorateElement:true });