<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-debug.js"></script>
<script src="https://rawgit.com/Knockout-Contrib/Knockout-Validation/master/Dist/knockout.validation.js"></script>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootswatch/3.3.1/united/bootstrap.min.css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/idea.min.css">
<div class="example col-sm-12">
<div class="page-header">
<h1><span class="text-danger">Required</span> Rule</h1>
</div>
<div class="alert alert-warning">
<span>The rule is used to ensure an observable has a value.</span>
</div>
<div role="tabpanel">
<ul class="nav nav-tabs">
<li class="active">
<a href="#example-code" role="tab" data-toggle="tab">Code</a>
</li>
<li>
<a href="#example-output" role="tab" data-toggle="tab">Output</a>
</li>
<li>
<a href="#example-inspect" role="tab" data-toggle="tab">Inspect</a>
</li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="example-code">
<script type="text/html">
<pre><code class="js">// Configure library to show messages as you type
ko.validation.init({
// We want the messages to show right away
messagesOnModified: false,
// We want to decorate the input element right away
decorateInputElement: true,
decorateElementOnModified: false
});
var viewModel = {
name: ko.observable('').extend({required: true})
};
ko.applyBindings(viewModel);</code></pre>
</script>
</div>
<div role="tabpanel" class="tab-pane"...
JavaScript
hljs.initHighlightingOnLoad();
jQuery('script[type="text/html"]').each(function () {
jQuery(this).parent().html(jQuery(this).html());
});
// Configure library to show messages as you type
ko.validation.init({
// We want the messages to show right away
messagesOnModified: false,
// We want to decorate the input element right away
decorateInputElement: true,
decorateElementOnModified: false,
// Since we are using bootstrap, let's use some custom classes
errorMessageClass: 'help-block',
errorElementClass: 'has-error'
});
var viewModel = {
name: ko.observable('').extend({
required: true
})
};
ko.applyBindings(viewModel);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.