Skip to main content

alphaDash

Validate that a string contains only Unicode alphanumeric characters plus dashes and underscores.

Signature

NguardValidators.String.alphaDash(hasAsciiOnly?: boolean): ValidatorFn
ParameterTypeDefaultDescription
hasAsciiOnlybooleanfalseIf 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 regex for finer control.

See also

  • alpha — letters only, no digits or dashes
  • alphaNum — letters and digits only, no dashes