Last active
August 29, 2015 14:23
-
-
Save laughingman7743/d58699789d445a2fd4f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container"> | |
<form class="form-horizontal"> | |
<h1>Form</h1> | |
<div class="form-group"> | |
<label class="col-sm-2 control-label" for="inputEmail">Email</label> | |
<div class="col-sm-10" data-bind="validationElement: email"> | |
<input type="text" id="inputEmail" class="form-control" placeholder="Email" data-bind="textinput: email"/> | |
<p class="text-danger" data-bind="validationMessage: email"></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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
ko.validation.rules['emailLocalPartMaxLength'] = { | |
validator: function (val, params) { | |
if(ko.validation.utils.isEmptyVal(val)) { | |
return true; | |
} | |
var normalizedVal = ko.validation.utils.isNumber(val) ? ('' + val) : val; | |
var emails = normalizedVal.split('@') | |
if(emails.length != 2) { | |
return true; | |
} | |
return emails[0].length <= params; | |
}, | |
message: 'message' | |
}; | |
ko.validation.registerExtenders(); | |
function myViewModel() { | |
var self = this; | |
self.email = ko.observable().extend({ | |
email: { | |
message: 'invalid format' | |
}, | |
emailLocalPartMaxLength: { | |
params: 10, | |
message: 'local part too long' | |
} | |
}); | |
} | |
ko.validation.init({ | |
insertMessages: false, | |
decorateElement: true, | |
errorElementClass: 'has-error' | |
}); | |
var viewModel = ko.validatedObservable(new myViewModel()); | |
ko.applyBindings(viewModel); | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: KnockoutJS Validation Sample 3 | |
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