Skip to content

Instantly share code, notes, and snippets.

@laughingman7743
Last active August 29, 2015 14:23
Show Gist options
  • Save laughingman7743/7b00fa3b5899af12c317 to your computer and use it in GitHub Desktop.
Save laughingman7743/7b00fa3b5899af12c317 to your computer and use it in GitHub Desktop.
@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css');
<div class="container">
<form class="form-horizontal">
<h1>Form</h1>
<div class="form-group">
<label class="col-sm-2 control-label" for="input1">Input1</label>
<div class="col-sm-10" data-bind="validationElement: input1">
<input type="text" id="input1" class="form-control" placeholder="input1" data-bind="value: input1"/>
<p class="text-danger" data-bind="validationMessage: input1"></p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input2">Input2</label>
<div class="col-sm-10" data-bind="validationElement: input2">
<input type="text" id="input2" class="form-control" placeholder="input2" data-bind="value: input2"/>
<p class="text-danger" data-bind="validationMessage: input2"></p>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="input3">Input3</label>
<div class="col-sm-10" data-bind="validationElement: input3">
<input type="text" id="input3" class="form-control" placeholder="input3" data-bind="value: input3"/>
<p class="text-danger" data-bind="validationMessage: input3"></p>
</div>
</div>
<div class="form-group well well-lg" >
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Submit</button>
<button type="button" class="btn">Cancel</button>
</div>
</div>
</form>
</div>
$(function() {
function myViewModel() {
var self = this;
self.input1 = ko.observable();
self.input2 = ko.observable();
self.input3 = ko.observable();
self.hasValue = ko.computed(function() {
return (self.input1() || self.input2() || self.input3());
});
self.input1.extend({
required: {
message: 'required',
onlyIf: function () {
if (self.hasValue()) {
return true;
}
return false;
}
}
});
self.input2.extend({
required: {
message: 'required',
onlyIf: function () {
if (self.hasValue()) {
return true;
}
return false;
}
}
});
self.input3.extend({
required: {
message: 'required',
onlyIf: function () {
if (self.hasValue()) {
return true;
}
return false;
}
}
});
}
ko.validation.init({
insertMessages: false,
messagesOnModified: false,
decorateElement: true,
errorElementClass: 'has-error'
});
var viewModel = ko.validatedObservable(new myViewModel());
ko.applyBindings(viewModel);
})
name: KnockoutJS Validation Sample 4
authors:
- laughingman7743
resources:
- http://knockoutjs.com/downloads/knockout-3.3.0.js
- http://code.jquery.com/jquery-2.1.4.min.js
- http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js
- http://cdnjs.cloudflare.com/ajax/libs/knockout-validation/2.0.3/knockout.validation.min.js
normalize_css: no
wrap: b
panel_js: 0
panel_css: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment