Confirm password validation angularjs material design - angularjs

I want to display an error using angularjs, under the "confirm password" input which should say "Passwords don't match".
<form name="settingsForm" ng-submit="vm.login(vm.credentials)" novalidate>
<md-input-container class="md-block" flex-gt-sm="">
<label translate="MY_PROFILE_NEW_PASSWORD"></label>
<input type="password" name="newPassword" id="password"
ng-model="vm.newPassword"
required=""
ng-minlength="8"
md-maxlength="40">
<div ng-messages="settingsForm.newPassword.$error"
role="alert"
multiple="">
<div ng-message="required"
class="my-message"
translate="LOGIN_PASSWORD_INVALID">
</div>
<div ng-message="minlength"
class="my-message"
translate="MIN_8_CHARS">
</div>
</div>
</md-input-container>
<md-input-container class="md-block" flex-gt-sm="">
<label translate="MY_PROFILE_CONFIRM_NEW_PASSWORD"></label>
<input type="password"
name="confirmPassword"
id="confirmPassword"
ng-model="vm.confirmPassword"
required=""
confirm-password="vm.newPassword"
ng-minlength="8"
md-maxlength="40">
<div ng-messages="settingsForm.$error.confirmPassword"
role="alert" multiple="" >
<div ng-message="required"
class="my-message"
translate="LOGIN_PASSWORD_INVALID">
</div>
<div ng-message="minlength"
class="my-message"
translate="MIN_8_CHARS">
</div>
</div>
<div ng-message="required"
class="my-message"
translate="LOGIN_PASSWORD_INVALID">
</div>
<div ng-message="confirm-password"
class="my-message">
Passwords don't match.
</div>
</md-input-container>
<md-button class="md-raised md-primary btn-auth"
type="submit"
ng-disabled="settingsForm.$invalid"
translate="SUBMIT"
ng-click="vm.changePassword(vm)">
</md-button>
I need to make this form display error messages for each particular case.
I haven't found how "confirm-password" attribute should work for validation.

You have to create a Custom Directive. Try this.
<div data-ng-controller="PasswordController as vmPassword">
<form name="passwordForm">
<md-input-container class="md-block" flex-gt-sm>
<label>Password</label>
<input type="password"
name="password"
ng-model="vmPassword.password"
required
/>
<div ng-messages="passwordForm.password.$error" role="alert" class="form-errors first-name-error">
<div ng-message="required">Password is required.</div>
</div>
</md-input-container>
<md-input-container class="md-block" flex-gt-sm>
<label>Confirm Password</label>
<input type="password"
name="confirm_password"
ng-model="vmPassword.confirm_password"
required
compare-to="vmPassword.password"
/>
<div ng-messages="passwordForm.confirm_password.$error" role="alert" class="form-errors first-name-error">
<div ng-message="required">Password is required.</div>
<div ng-message="compareTo">Passwords do not match</div>
</div>
</md-input-container>
</form>
</div>
Custom Directive
(function () {
"use strict";
angular.module('app').directive('compareTo', compareTo);
compareTo.$inject = [];
function compareTo() {
return {
require: "ngModel",
scope: {
compareTolValue: "=compareTo"
},
link: function(scope, element, attributes, ngModel) {
ngModel.$validators.compareTo = function(modelValue) {
return modelValue == scope.compareTolValue;
};
scope.$watch("compareTolValue", function() {
ngModel.$validate();
});
}
};
}
})();
Goodluck .! :)
Demo here - http://embed.plnkr.co/UK4G4Lm5BCNNe5SWoA9r/

Related

how to close modal after submit in angularjs

i know have many result in this question but no one give work answer for me.
html 1:
<button class="basebtn logbtn" ng-click="openlog()">LOGIN</button>
<button class="basebtn regbtn" ng-click="openreg()">SIGN UP</button>
html 2:
<div ng-controller="main">
<center><img class="img-login" ng-src="../images/form-logo.png"/></center>
<form class="login" name="log" ng-submit="login()" novalidate>
<div class="form-group login-form" ng-class="{'has-error' : isInvalid(log.email) ,'has-success' : isValid(log.email)}">
<input type="email" class="form-control" name="email" ng-model="user.email" required placeholder="Email">
<div class="alert alert-danger" role="alert" ng-show="isInvalid(log.email)">Enter valid email</div>
</div>
<div class="form-group login-form" ng-class="{'has-error' : isInvalid(log.password) ,'has-success' : isValid(log.password)}">
<input type="password" class="form-control" name="password" ng-model="user.password" required placeholder="Password">
<div class="alert alert-danger" role="alert" ng-show="isInvalid(log.password)">This Feild is required</div>
</div>
<div class="form-group" style="text-align:center;">
<input type="submit" class="logbnt" value="LOGIN" ng-disabled="! log.$valid" data-dismiss='modal'/>
</div>
</form>
angular :
$scope.login = function(){
$http.post("www.example.com/login",$scope.user).then(function(res){
localStorage.setItem("token",res.data.token);
//how i close the modal ???
});
}
//here create the modal
$scope.openlog = function () {
$scope.$modalInstance = $uibModal.open({
templateUrl:"login.html",
});
}
that my code , how i close in $http result the modal ?
You can do this,
$scope.login = function(){
$http.post("www.example.com/login",$scope.user).then(function(res){
localStorage.setItem("token",res.data.token);
$modalInstance.dismiss('cancel');
});
};

ng-messages dont reset when resetting the form

I have a big problem in my form when I try to reset it everything is reset except the ng messages. I tried everything but nothing worked. I don't know why
Here is my form:
<md-content layout-padding>
<form name="projectForm">
<div layout="row">
<md-input-container flex="50">
<label>Name</label>
<input required name="clientName" ng-model="project.clientName" ng-focus="">
</md-input-container>
<md-input-container flex="50">
<label>Email</label>
<input required type="email" name="clientEmail" ng-model="newEmployee.email"
minlength="10" maxlength="100" ng-pattern="/^.+#.+\..+$/" />
<div ng-messages="projectForm.clientEmail.$error" role="alert" ng-show="message">
<div ng-message-exp="['required', 'minlength', 'maxlength', 'pattern']">
Your email must be between 10 and 100 characters long and look like an e-mail address.
</div>
</div>
</md-input-container>
</div>
<div layout="row">
<md-input-container flex="50">
<label>Mobile</label>
<input required name="clientName" ng-model="newEmployee.mobile">
<div ng-messages="projectForm.clientName.$error" ng-show="message">
<div ng-message="required">This is required.</div>
</div>
</md-input-container>
<md-input-container flex="50">
<label>Address</label>
<input required name="clientName" ng-model="newEmployee.address">
<div ng-messages="projectForm.clientName.$error" ng-show="message">
<div ng-message="required">This is required.</div>
</div>
</md-input-container>
</div>
<md-input-container class="md-block">
<label>Description</label>
<input md-maxlength="30" required md-no-asterisk name="description" ng-model="newEmployee.description">
<div ng-messages="projectForm.description.$error">
<div ng-message="required">This is required.</div>
<div ng-message="md-maxlength">The description must be less than 30 characters long.</div>
</div>
</md-input-container>
<div>
<md-button type="submit" class="md-raised md-primary">Submit</md-button>
<md-button ng-click="cancel()">Cancel</md-button>
</div>
</form>
</md-content>
and here is my reset function in the controller
$scope.reset = function() {
$scope.projectForm.$setPristine();
$scope.projectForm.$setValidity();
$scope.projectForm.$setUntouched();
}

AngularJS set form to initial state

I have a form which once submitted, I want to clear all fields and set to it initial state.
I use $scope.form.setPristine(); but field labels keep red color.
How can I avoid this?
EDIT:
Here i post the code. All works fine except by such issue.
Html:
<form name="change_password" novalidate>
<md-input-container style="margin:0;width:200px" flex>
<label>Enter your current password</label>
<input name="password" type="password" ng-model="password" ng-minlength="7" required>
<div ng-messages="change_password.password.$error" ng-if="change_password.password.$dirty" role="alert">
<div class="error_form" ng-message="required">Enter your current password.</div>
<div class="error_form" ng-message="minlength">Password must be at least 7 characters long.</div>
</div>
</md-input-container>
<md-input-container style="margin:0;width:200px" flex>
<label>New Password</label>
<input name="new_password" type="password" ng-model="new_password" ng-minlength="7" required>
<div ng-messages="change_password.new_password.$error" ng-if="change_password.new_password.$dirty" role="alert">
<div class="error_form" ng-message="required">Enter your new password.</div>
<div class="error_form" ng-message="minlength">New password must be at least 7 characters long.</div>
</div>
</md-input-container>
<md-input-container style="margin:0;width:200px" flex>
<label>Confirm new password</label>
<input name="re_new_password" type="password" ng-model="re_new_password" ng-minlength="7" required>
<div ng-messages="change_password.re_new_password.$error" ng-if="change_password.re_new_password.$dirty" role="alert">
<div class="error_form" ng-message="required">Confirm your new password.</div>
<div class="error_form" ng-message="minlength">New password must be at least 7 characters long.</div>
</div>
</md-input-container>
<button class="button" style="width:200px" ng-if="!saving" ng-click="save_password()" ng-disabled="is_uploading || change_password.$invalid || new_password!=re_new_password">Save new password</button>
<div ng-if="saving" layout="row" layout-align="center center">
<md-progress-circular md-mode="indeterminate" size="22"></md-progress-circular>
</div>
</form>
Ctrl:
userService.save_password($scope.password, $scope.new_password).then( function(response) {
$scope.$apply( function() {
if ( response.result ) {
$scope.password = '';
$scope.new_password = '';
$scope.re_new_password = '';
console.info($scope.change_password);
$scope.change_password.$setPristine();
}
showMessage(response);
$scope.saving = false;
})
})
You misunderstand
setPristine();
It only clears classes on your form, nothing else. Variables are still set. You need to do it like below:
$scope.emptyModel = {};
$scope.reset = function() {
$scope.yourFormModel = angular.copy($scope.emptyModel);
$scope.user_form.$setPristine();
$scope.user_form.$setUntouched();
}
Reset with $setPristine and $setUntouched (for your fiddle):
$scope.user_form.$setPristine();
$scope.user_form.$setUntouched();

i want to validate my form if the mobile no is already present in back end

i'm getting the response from back end that the mobile no is already present or is a new mobile no.but unable to to show the validation in my form that the mobile no is already registered.can anyone help me with that.
<div>
<md-content ng-app="shoppingPad" ng-controller="loginController">
<p class="Welcome-to-ShoppingP">Welcome to ShoppingPad</p>
<div>
<form name="myForm" ng-app="shoppingPad" class="myForm" ng-submit="submit()">
<div class="row formData">
<div class="col-xs-8">
<md-input-container>
<md-icon md-svg-icon="assets/images/device.svg"></md-icon>
<input type="number" placeholder="Enter user mobile no" name="mobile" id="mobile" ng-model="mobile" required phone-number-validator minlength="10">
<div ng-messages="myForm.mobile.$error">
<div ng-message="required">Registered mobile no.</div>
<div ng-message="phoneNumber">The mobile No entered has already been registered</div>
<div ng-message="test">too short</div>
</div>
</md-input-container>
</div>
<div class="col-xs-8">
<md-input-container>
<md-icon md-svg-icon="assets/images/lock.svg"></md-icon>
<input type="password" placeholder="Enter 3-D secure pin" name="password" id="password" ng-model="password" required minlength="6">
<div ng-messages="myForm.password.$error">
<div ng-message="required">Enter Pin no.</div>
<div ng-message="minlength">too short</div>
</div>
</md-input-container>
</div>
</div>
<!-- end of login form-->
<!-- login toolbar-->
<div>
<md-toolbar class="loginToolBar">
<md-button class="md-primary" ng-disabled="myForm.$invalid" style="margin-top: 15px;outline: none;" ng-click="login()">LOGIN</md-button>
</md-toolbar>
</div>
</form>
</div>
</md-content>
</div>
angular.module('shoppingPad').directive('phoneNumberValidator',phoneNumberValidator)
function phoneNumberValidator($q,restService){
var deferred=$q.defer();
return{
restrict:'EA',
require:'ngModel',
link:function($scope,element,attrs,ngModel){
ngModel.$asyncValidators.phoneNumber=function(modelValue , viewValue) {
var mobile= (modelValue || viewValue).toString();
console.log(mobile);
ngModel.$asyncValidators.userMobile=function(mobile){
return restService.getRequest('save/all',null).then(function(response){
console.log("inside get");
return deferred.promise;
},
function(error){
deferred.reject(error);
return deferred.promise;
}
)
};
}
}
}
}

form.$valid always returning false angularjs

I'm trying to make validations to my form in an ng-repeat which now always returning as false even though all the inputs are filled and correct.
Here's my update function:
$scope.updateChannel = function (channel) {
if (channelForm.$valid) {
Channels.update({
_id: channel._id
}, {
$set: {
name: channel.name,
category: channel.category,
tags: channel.tags,
logo: channel.logo
}
}, function (error, data) {
if (error) {
console.log('unable to update the channel: ' + error);
} else {
console.log('Done! ' + data);
}
});
} else {
console.log('fix errors');
}
}
and here's my form:
<form ng-show="channel.edit == true" name="channelForm" ng-submit="updateChannel(channel)" novalidate>
<md-input-container>
<label for="name">Nombre</label>
<input type="text" name="name" ng-model="channel.name" md-maxlength="20" required>
<div ng-messages="channelForm.name.$error;">
<div ng-message="required">Esto es requerido.</div>
<div ng-message="md-maxlength">Demaciados carácteres.</div>
</div>
</md-input-container>
<md-input-container class="md-block">
<label for="category">Categorias</label>
<md-select name="category" ng-model="channel.category" multiple required>
<md-option ng-repeat="kit in kits" value="{{kit.name}}">{{kit.name}}</md-option>
</md-select>
<div class="errors" ng-messages="channelForm.category.$error">
<div ng-message="required">Esto es requerido.</div>
</div>
</md-input-container>
<md-input-container>
<label for="number">Canal</label>
<input type="number" name="number" ng-model="channel.number" ng-model-options="{ updateOn: 'blur' }" maxlength="5" required>
<div ng-messages="channelForm.number.$error;">
<div ng-message="required">Esto es requerido.</div>
<div ng-message="md-maxlength">Demaciados carácteres.</div>
</div>
</md-input-container>
<md-input-container>
<label for="tags">Tags(separar con espacio)</label>
<input type="text" name="tags" ng-model="channel.tags" md-maxlength="20" required>
<div ng-messages="channelForm.tags.$error;">
<div ng-message="required">Esto es requerido.</div>
<div ng-message="md-maxlength">Demaciados carácteres.</div>
</div>
</md-input-container>
<md-input-container>
<label for="logo">Logo</label>
<input type="text" name="logo" ng-model="channel.logo" required>
<div ng-messages="channelForm.logo.$error;">
<div ng-message="required">Esto es requerido.</div>
</div>
</md-input-container>
<md-button type="submit" class="md-raised md-primary" ng-click="channel.edit = false" ng-disabled="channelForm.$invalid">Guardar</md-button>
<md-button class="md-raised" ng-click="channel.edit = false">Cancelar</md-button>
<md-button class="md-raised md-warn" ng-click="removeChannel(channel)">Borrar</md-button>
</form>
now this is in an ng-repeat="channel in channels" so I'm not sure if that's the problem.
Also, all the ng-messages seem to be working correctly and even when none of them are showing I'm still unable to update the document.
What am I doing wrong here?
Thanks!
If this is in a ng-repeat, you will end up with a bunch of forms named channelForm
You could put a new controller on the form tag and move updateChannel in that controller.

Resources