alphaDash
Validate that a string contains only Unicode alphanumeric characters plus dashes and underscores.
Signature
NguardValidators.String.alphaDash(hasAsciiOnly?: boolean): ValidatorFn
| Parameter | Type | Default | Description |
|---|---|---|---|
hasAsciiOnly | boolean | false | If true, restricts characters to ASCII alphanumerics (a-z, A-Z, 0-9) plus dash and underscore |
Reactive forms
import { FormControl } from '@angular/forms';
import { NguardValidators } from 'ng-nguard';
const username = new FormControl('', [NguardValidators.String.alphaDash()]);
// ASCII-only username
const asciiUsername = new FormControl('', [NguardValidators.String.alphaDash(true)]);
Template-driven forms
<input ngModel name="username" nguardAlphaDash />
<input ngModel name="ascii" [nguardAlphaDash]="{ hasAsciiOnly: true }" />
Error key
{ alphaDash: true }
Notes
- Default Unicode mode allows any letter/digit plus
-and_. - ASCII mode restricts to
[a-zA-Z0-9_-]+. - Empty strings fail.
- Spaces fail — use
regexfor finer control.