Skip to main content

alphaNum

Validate that a string contains only Unicode alphanumeric characters (letters and digits).

Signature

NguardValidators.String.alphaNum(hasAsciiOnly?: boolean): ValidatorFn
ParameterTypeDefaultDescription
hasAsciiOnlybooleanfalseIf true, restricts characters to ASCII alphanumerics (a-z, A-Z, 0-9)

Reactive forms

import { FormControl } from '@angular/forms';
import { NguardValidators } from 'ng-nguard';

const code = new FormControl('', [NguardValidators.String.alphaNum()]);
const ascii = new FormControl('', [NguardValidators.String.alphaNum(true)]);

Template-driven forms

<input ngModel name="code" nguardAlphaNum />
<input ngModel name="ascii" [nguardAlphaNum]="{ hasAsciiOnly: true }" />

Error key

{ alphaNum: true }

Notes

  • Default Unicode mode uses \p{L}\p{M}\p{N} — letters, combining marks, and any numeric character.
  • ASCII mode restricts to [a-zA-Z0-9]+.
  • Empty strings fail.
  • No dashes, underscores, or whitespace.

See also

  • alpha — letters only, no digits
  • alphaDash — adds dashes and underscores