<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.js"></script>
<script src="https://rawgit.com/dscheerens/angular-conditional-validation/master/dist/angular-conditional-validation.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body ng-app="demoApp">
<div class="container">
<h1>Angular conditional validation demos</h1>
<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title">Simple on/off validation</h2>
</div>
<div class="panel-body">
<form name="form1" ng-controller="Form1Controller as ctrl">
<div class="form-group" ng-class="{'has-error': form1.$invalid}">
<label for="exampleInputEmail1">Text input</label>
<input
type="text"
class="form-control"
name="field"
ng-model="ctrl.textValue"
required
ng-minlength="4"
ng-maxlength="5"
pattern="abcd"
enable-validation="ctrl.enableValidation"
>
</div>
<div class="checkbox">
<label>
<input type="checkbox" ng-model="ctrl.enableValidation"> Enable validation
</label>
</div>
<table class="table table-bordered">
<tr>
<th>Validator</th>
<th>Enabled</th>
<th>Status</th>
</tr>
<tr>
<td><code>required</code></td>
<td><status value="!!ctrl.enableValidation" positive="YES" negative="NO"></status></td>
<td><status value="!form1.field.$error.required" positive="OK" negative="ERROR"></status></td>
</tr>
<tr>
<td><code>ng-minlength="4"</code></td>
<td><status value="!!ctrl.enableValidation" positive="YES" negative="NO"></status></td>
<td><status value="!form1.field.$error.minlength" positive="OK" negative="ERROR"></status></td>
</tr>
<tr>
<td><code>ng-maxlength="5"</code></td>
<td><status value="!!ctrl.enableValidation" positive="YES" negative="NO"></status></td>
<td><status value="!form1.field.$error.maxlength" positive="OK" negative="ERROR"></status></td>
</tr>
<tr>
<td><code>pattern="abcd"</code></td>
<td><status value="!!ctrl.enableValidation" positive="YES" negative="NO"></status></td>
<td><status value="!form1.field.$error.pattern" positive="OK" negative="ERROR"></status></td>
</tr>
</table>
</form>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title">Individual validator selection</h2>
</div>
<div class="panel-body">
<form name="form2" ng-controller="Form2Controller as ctrl">
<div class="form-group" ng-class="{'has-error': form2.$invalid}">
<label for="exampleInputEmail1">Text input</label>
<input
type="text"
class="form-control"
name="field"
ng-model="ctrl.textValue"
required
ng-minlength="4"
ng-maxlength="5"
pattern="abcd"
enable-validation="ctrl.enableValidation"
>
</div>
<hr>
<div class="checkbox">
<label>
<input type="checkbox" ng-model="ctrl.enableValidation.minlength"> Enable <code>minlength</code> validator
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" ng-model="ctrl.enableValidation.maxlength"> Enable <code>maxlength</code> validator
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" ng-model="ctrl.enableValidation['*']"> Enable other validators
</label>
</div>
<hr>
<table class="table table-bordered">
<tr>
<th>Validator</th>
<th>Enabled</th>
<th>Status</th>
</tr>
<tr>
<td><code>required</code></td>
<td><status value="!!ctrl.enableValidation['*']" positive="YES" negative="NO"></status></td>
<td><status value="!form2.field.$error.required" positive="OK" negative="ERROR"></status></td>
</tr>
<tr>
<td><code>ng-minlength="4"</code></td>
<td><status value="!!ctrl.enableValidation.minlength" positive="YES" negative="NO"></status></td>
<td><status value="!form2.field.$error.minlength" positive="OK" negative="ERROR"></status></td>
</tr>
<tr>
<td><code>ng-maxlength="5"</code></td>
<td><status value="!!ctrl.enableValidation.maxlength" positive="YES" negative="NO"></status></td>
<td><status value="!form2.field.$error.maxlength" positive="OK" negative="ERROR"></status></td>
</tr>
<tr>
<td><code>pattern="abcd"</code></td>
<td><status value="!!ctrl.enableValidation['*']" positive="YES" negative="NO"></status></td>
<td><status value="!form2.field.$error.pattern" positive="OK" negative="ERROR"></status></td>
</tr>
</table>
</form>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title">Function based validation condition</h2>
</div>
<div class="panel-body">
<form name="form3" ng-controller="Form3Controller as ctrl">
<div class="form-group" ng-class="{'has-error': form3.$invalid}">
<label for="exampleInputEmail1">Number input</label>
<input
type="number"
class="form-control"
name="field"
ng-model="ctrl.numberValue"
ng-minlength="4"
ng-maxlength="4"
pattern="1337"
enable-validation="ctrl.enableValidation"
>
</div>
<hr>
<div class="checkbox">
<label>
<input type="checkbox" ng-model="ctrl.enableValidation.minlength"> Enable <code>minlength</code> validator
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" ng-model="ctrl.enableValidation.maxlength"> Enable <code>maxlength</code> validator
</label>
</div>
<em class="text-muted">Enable the <code>minlength</code> and <code>maxlength</code> validators to enable the <code>pattern</code>validator.</em>
<hr>
<table class="table table-bordered">
<tr>
<th>Validator</th>
<th>Enabled</th>
<th>Status</th>
</tr>
<tr>
<td><code>ng-minlength="4"</code></td>
<td><status value="!!ctrl.enableValidation.minlength" positive="YES" negative="NO"></status></td>
<td><status value="!form3.field.$error.minlength" positive="OK" negative="ERROR"></status></td>
</tr>
<tr>
<td><code>ng-maxlength="4"</code></td>
<td><status value="!!ctrl.enableValidation.maxlength" positive="YES" negative="NO"></status></td>
<td><status value="!form3.field.$error.maxlength" positive="OK" negative="ERROR"></status></td>
</tr>
<tr>
<td><code>pattern="1337"</code></td>
<td><status value="!!ctrl.enableValidation.pattern()" positive="YES" negative="NO"></status></td>
<td><status value="!form3.field.$error.pattern" positive="OK" negative="ERROR"></status></td>
</tr>
</table>
</form>
</div>
</div>
</div>
</body>
</html>
var app = angular.module('demoApp', ['angularConditionalValidation']);
app.controller('Form1Controller', function() {
this.textValue = 'abc';
this.enableValidation = true;
});
app.controller('Form2Controller', function() {
this.textValue = 'abc';
this.enableValidation = {
minlength: true,
maxlength: true,
'*': false
};
});
app.controller('Form3Controller', function() {
this.numberValue = 133;
this.enableValidation = {
minlength: false,
maxlength: true,
pattern: (function() {
return this.enableValidation.minlength && this.enableValidation.maxlength;
}).bind(this)
};
});
app.directive('status', function() { return {
restrict: 'E',
scope: {},
bindToController: {
value: '=',
positive: '@',
negative: '@'
},
controller: function() {
},
controllerAs: 'ctrl',
template: (
'<span class="text-success" ng-show="ctrl.value">' +
'<span class="glyphicon glyphicon-ok"></span> {{ ctrl.positive }}' +
'</span>' +
'<span class="text-danger" ng-show="!ctrl.value">' +
'<span class="glyphicon glyphicon-remove"></span> {{ ctrl.negative }}' +
'</span>'
)
};});
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |