I need to verify the username based on the following criteria:
Only accept alphanumeric characters
Allow either "_" or "." (but not both)
This is the code snippet I am currently using:
<input type="text" class="form-control" [maxlength]="50" name="userName" placeholder="User Name"
#userName="ngModel" ngModel [(ngModel)]="UserName" (keypress)="userNameValidation($event)">
userNameValidation(event: any) {
const pattern = /^[a-zA-Z0-9\_\.]+$/
const inputChar = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (!pattern.test(inputChar)) {
event.preventDefault();
}
}
Could someone assist me in implementing the "either/or" condition?