1

Hi using angular 2 for password field I have to set " Password should have at least 1 uppercase, 1 lowercase, 1 number, 1 special character, minimum of 8 characters, and maximum of 16 characters"

In that I have done "minimum of 8 characters, and maximum of 16 characters".

But I am unable to set for remaining things least 1 uppercase, 1 lowercase, 1 number, 1 special character.

<div class ="form-group">
  <label> Password </label>
   <input class ="form-control" name ="password" #password="ngModel" [(ngModel)] = "angulaService.selectedAngula.password" 
   placeholder="password"  minlength="8" maxlength="16" >

   <div class="form-control-feedback"
   *ngIf="password.errors && (password.dirty || password.touched)">
  <p *ngIf="password.errors.required">Password is required</p>
  <p *ngIf="password.errors.minlength">Password must be min  8 characters long</p>
 </div>
  <div class="form-control-feedback"
    *ngIf="password.errors && (password.dirty || password.touched)">
    <p *ngIf="password.errors.required">Password is required</p>
    <p *ngIf="password.errors.maxlength">Password should not be greater than  16 characters long</p>
  </div>
</div>

Please help me in this

Thanks in Advance

4
  • Use async validators. Commented Mar 30, 2018 at 20:29
  • Is this for Angular 1.x or 2.x+? Please make sure you don't mix the tags. Commented Mar 30, 2018 at 23:45
  • @sundeep: Please correct the tags, they are mixed. Commented Mar 31, 2018 at 0:20
  • This is for Angular 2.x+ Commented Mar 31, 2018 at 1:30

2 Answers 2

1

Example of uppercase validator:

import { Directive } from '@angular/core';
import {NG_VALIDATORS, AbstractControl, ValidationErrors, Validator} from '@angular/forms';

@Directive({
  selector: '[uppercaseValidator]',
  providers: [{provide: NG_VALIDATORS, useExisting: UppercaseValidatorDirective, multi: true}]
})
export class UppercaseValidatorDirective implements Validator {
  validate(control: AbstractControl): ValidationErrors | null {
    const value = control.value
    return /[A-Z]/.test(value) ? null : { uppercase: 'Must contain at least one upper case character'}
  }
}

Usage:

app.component.html

<form>
  <input uppercaseValidator [(ngModel)]="uppercase" #uppercaseCtrl="ngModel" name="uppercase"/>
  <div *ngIf="uppercaseCtrl.hasError('uppercase')">At least one character must be uppercased</div>
</form>

app.component.ts

<form>
  <input uppercaseValidator [(ngModel)]="uppercase" #uppercaseCtrl="ngModel" name="uppercase"/>
  <div *ngIf="uppercaseCtrl.hasError('uppercase')">At least one character must be uppercased</div>
</form>

Live demo

Create other validators by following the same steps of creating a custom validation directive. You just need to change the regexp and error message. You can use following regexp:

  • uppercase: /[A-Z]/
  • lowercase: /[a-z]/
  • number: /[0-9]/
  • special character: depends on what you consider a special character.
Sign up to request clarification or add additional context in comments.

Comments

0

Better use Regex for this,

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]

(you are already checking for min length and max length, so no need to put length limitation in regex, but you can still do that via regex. Although I would suggest keep both of the error separated.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.