验证密码字段是否存在空白

时间:2019-01-03 07:32:08

标签: regex angular angular7 angular-forms

我正在尝试使用password方法针对empty spaces可用性验证[pattern]字段。但没有得到结果。

这是ts文件:

@Component({
    selector: 'app-sign-up',
    templateUrl: './sign-up.component.html',
    styleUrls: ['./sign-up.component.css']
})
export class SignUpComponent implements OnInit {

    patternName = /^[a-zA-Z ]{3,}$/;
    patternUsername = /^[a-z]{3,}$/;
    patternPassword = /\s/; //selects empty space if any..

    constructor( private userService:UserService ) { }

    ngOnInit() {}

}

html:

<div class="form-group"> 
  <label class="control-label col-sm-2" for="password">Password</label>
  <input type="password" 
         class="col-sm-4" 
         #password="ngModel"
         name="password" 
         [pattern]="!patternPassword" 
         id="password" 
         placeholder="Enter Password"
         [(ngModel)]="userService.selectedUser.password" 
         required>

  <div *ngIf="formSignin.submitted && !password.valid">
    <span *ngIf="password.errors.required">Password required</span>
  </div>
  //not getting while add space!?
  <div *ngIf="password.errors?.pattern">Space not allowed</div>
</div>
<div class="form-group"> 
  <button type="submit" class="btn btn-default">Submit</button>
</div>

0 个答案:

没有答案