Skip to content
trminator edited this page Nov 29, 2016 · 9 revisions

required:

var myObj = ko.observable().extend({ required: true });
var myObj = ko.observable().extend({ required: { onlyIf: function() { return true; } } });
var myObj = ko.observable().extend({ required: { params: true, message: 'This field is required.' } });

min:

var myObj = ko.observable().extend({ min: 2 });

max:

var myObj = ko.observable().extend({ max: 99 });

minLength:

var myObj = ko.observable().extend({ minLength: 3 });

maxLength:

var myObj = ko.observable().extend({ maxLength: 12 });

pattern:

var myObj = ko.observable().extend({ pattern: '^[a-z0-9].$' });

step: Works really well with min and max

var myObj = ko.observable().extend({ step: 3 });

email:

var myObj = ko.observable().extend({ email: true });

phoneUS:

var myObj = ko.observable().extend({ phoneUS: true });

equal:

var otherObj = ko.observable();
var myObj = ko.observable().extend({ equal: otherObj });
//or
var myObj = ko.observable().extend({ equal: 2 }); // in case you just want it to equal something

notEqual:

var otherObj = ko.observable();
var myObj = ko.observable().extend({ notEqual: otherObj });
//or
var myObj = ko.observable().extend({ notEqual: 2 }); // in case you just want it to NOT equal something else

date:

var myObj = ko.observable().extend({ date: true });

dateISO:

var myObj = ko.observable().extend({ dateISO: true });

number:

var myObj = ko.observable().extend({ number: true });

digit:

var myObj = ko.observable().extend({ digit: true });

unique:

var myObj = ko.observable().extend({
    unique: {
        collection: myArray,
        externalValue: false,
        valueAccessor: function(item) { return item.property; }
    }
});

Options are:

  • collection: array or function returning (observable) array in which the value has to be unique
  • valueAccessor: function that returns value from an object stored in collection. If it is null the value is compared directly
  • external: set to true when object you are validating is automatically updating collection