TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Validate two Fields Equality In AngularJS
Lokesh Boran
Feb 10
2016
Code
1.2
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
angular.module(
'app'
).directive(
'validateConfirmField'
, validateConfirmField);
function validateConfirmField($parse)
{
return
{
require:
'ngModel'
,
link: function (scope, elem, attrs, ctrl)
{
scope.$watch(function ()
{
if
(!(ctrl.$modelValue) || !($parse(attrs.validateConfirmField)(scope)))
{
return
true
;
}
return
($parse(attrs.validateConfirmField)(scope)).toLowerCase() === ctrl.$modelValue.toLowerCase();
}, function (currentValue)
{
ctrl.$setValidity(
'validateConfirmField'
, currentValue);
});
}
};
}
<!-- HTML View part where you want to validate your fields -->
< input type =
"text"
name =
"name1"
value =
"Field1"
ng - model =
"field1"
> < input type =
"text"
name =
"name2"
value =
"Field2"
ng - model =
"field2"
data - validate - confirm - field =
"field1"
>
<!-- HTML Validation -->
< span ng - show =
"form.name2.$error.validateConfirmField"
> Field1 and Field2 does not match. < /span>
AngularJS
AngularJS Directive