我正在尝试使用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>