Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
HTML

Hi, I have designed a sign up form with name, password, confirm password, and e-mail id text boxes. i am doing client side validation using angular js. Name, E-mail and PAssowrd test box perform the function as expected and i am able to do client side validtion through angular js. But in the confirm password field when i enter a wrong password angular js fails to validate it. I do not know what went wrong with my code. I tried googling out solution but did not get proper answer for this. Kindly help me in this. Many thanks in advance. here is my code

Login.html

XML
<html>
<head>
    <!-- CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css" />
    <style>
        body     { padding-top:30px; }
    </style>

    <!-- JS -->
    <script src="js/angular.js"></script>
    <script src="js/app.js"></script>
</head>

<body ng-app="myapp">
<div class="container">

    <!-- PAGE HEADER -->
    <div class="page-header"><h1>AngularJS Form Validation</h1></div>

    <!-- pass in the variable if our form is valid or invalid -->
    <form name="userForm" ng-submit="submitForm(userForm.$valid)" novalidate>

        

        <!-- USERNAME -->
        <div class="form-group" ng-class="{ 'has-error' : userForm.Name.$invalid && !userForm.Name.$pristine }">
            <label>Name*</label>
            <input type="text" name="Name" class="form-control" placeholder="your preferred username" ng-model="user.Name" required ng-minlength="2" ng-maxlength="25">
            <p ng-show="userForm.Name.$error.minlength" class="help-block">Name is too short.</p>
            <p ng-show="userForm.Name.$error.maxlength" class="help-block">Name is too long.</p>
        </div>

        

        <div class="form-group" ng-class="{ 'has-error' : userForm.password.$invalid && !userForm.password.$pristine }">
        <label for="password">Password</label>
       <input type="password" id="password" class="form-control" name="password" ng-model="user.password" ng-minlength="8" ng-maxlength="20" ng-pattern="/(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])/" required  />
       <p ng-show="userForm.password.$error.required && userForm.password.$dirty" class="help-block">Required</p>
       <p ng-show="!userForm.password.$error.required && !userForm.password.$error.minlength && !userForm.password.$error.maxlength && userForm.password.$error.pattern && userForm.password.$dirty" class="help-block">Must contain one lower &amp; uppercase letter, and one non-alpha character (a number or a symbol)</p>
       <p ng-show="!userForm.password.$error.required && (userForm.password.$error.minlength || userForm.password.$error.maxlength) && userForm.password.$dirty" class="help-block">Passwords must be between 8 and 20 characters.</p>
       </div>

       <div class="form-group" ng-class="{ 'has-error' : userForm.password_c.$invalid && !userForm.password_c.$pristine }">
       <label for="password_c">Confirm Password</label>
       <input type="password" id="password_c" class="form-control" name="password_c" ng-model="user.password_c" valid-password-c required  />
       <p ng-show="userForm.password_c.$error.required && userForm.password_c.$dirty" class="help-block">Please confirm your password.</p>
       <p ng-show="!userForm.password_c.$error.required && userForm.password_c.$error.noMatch && userForm.password.$dirty" class="help-block">Passwords do not match</p>
       </div>


        <!-- EMAIL -->
        <div class="form-group" ng-class="{ 'has-error' : userForm.email.$invalid && !userForm.email.$pristine }">
            <label>Email*</label>
            <input type="email" name="email" class="form-control" placeholder="someone@123.com" ng-model="user.email" required>
            <p ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="help-block">Enter a valid email.</p>
        </div>

        <button type="submit" class="btn btn-primary">Sign Up</button>

    </form>
</div>
</body>
</html>





App.js

C#
var app = angular.module('myapp', ['UserValidation']);

angular.module('UserValidation', []).directive('validPasswordC', function () {
    return {
        require: 'ngModel',
        link: function (scope, elm, attrs, ctrl) {
            ctrl.$parsers.unshift(function (viewValue, $scope) {
                var noMatch = viewValue != scope.userForm.password.$viewValue
                ctrl.$setValidity('noMatch', !noMatch)
            })
        }
    }
})
Posted
Updated 17-Jul-14 0:35am
v5
Comments
Sergey Alexandrovich Kryukov 18-Jul-14 11:40am    
This is nothing but code dump, no problem is explained.
But your problem is: why comparing passwords at all? Storing passwords any where and comparing them is way too unsafe and absolutely not needed for authentication. Besides, why working on client side? Are you using any server-side technology? If not, you cannot really have safe password-based authentication.
—SA

1 solution

Creating a separate directive for this is not needed. There is already a build in Angular UI password validation tool .angular-ui/ui-validate · GitHub[^] With this you could do:

<input name="password" required ng-model="password">
<input name="confirm_password"
ui-validate=" '$value==password' "
ui-validate-watch=" 'password' ">
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900