Skip to content

Instantly share code, notes, and snippets.

@Lazhari
Created July 18, 2015 05:23
Show Gist options
  • Save Lazhari/0695d21b5ef7fee8503d to your computer and use it in GitHub Desktop.
Save Lazhari/0695d21b5ef7fee8503d to your computer and use it in GitHub Desktop.
Directive Angularjs : Confirm password
angular.module('app')
.directive('validateEquals', function () {
return {
require: 'ngModel',
link: function(scope, element, attrs, ngModelCtrl) {
function validateEqual(value) {
var valid = (value === scope.$eval(attrs.validateEquals));
ngModelCtrl.$setValidity('equal', valid);
return valid ? value: 'undefined';
}
ngModelCtrl.$parsers.push(validateEqual);
ngModelCtrl.$formatters.push(validateEqual);
scope.$watch(attrs.validateEquals, function() {
ngModelCtrl.$setViewValue(ngModelCtrl.$viewValue);
})
}
};
});
<form ng-submit="submit()" name="register" novalidate>
<h2 class="text-center">Register</h2>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input name="email" ng-model="email" type="email" class="form-control" id="exampleInputEmail1" placeholder="Email" required>
<p class="help-block" ng-show="register.email.$dirty && register.email.$invalid">Please enter a proper email.</p>
</div>
<div class="form-group">
<label for="passwordInput">Password</label>
<input name="password" ng-model="password" type="password" class="form-control" id="passwordInput" placeholder="Password" required>
</div>
<div class="form-group">
<label for="confirmPasswordInput">Confirm Password</label>
<input name="password_confirm" ng-model="password_confirm" type="password" class="form-control" id="confirmPasswordInput" placeholder="Confirm Password" validate-equals="password">
</div>
<p class="help-block" ng-show="register.password_confirm.$dirty && register.password_confirm.$invalid ">Please match the Passwords</p>
<div class="text-center">
<button type="submit" class="btn btn-primary btn-lg" ng-disabled="register.$invalid">Submit</button>
</div>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment