Skip to main content

startsWith

Validate that a string starts with at least one of the given substrings (case-insensitive).

Signature

NguardValidators.String.startsWith(...values: primitive[]): ValidatorFn
ParameterTypeDescription
...valuesprimitive[]One or more substrings — the input must start with at least one of them

Reactive forms

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

const url = new FormControl('', [
NguardValidators.String.startsWith('http://', 'https://'),
]);

Template-driven forms

<!-- Single value: pass directly -->
<input ngModel name="url" [nguardStartsWith]="'https://'" />

<!-- Multiple values: pass an array -->
<input ngModel name="url" [nguardStartsWith]="['http://', 'https://']" />

Error key

{ startsWith: true }

Notes

  • The check is case-insensitive.
  • Numbers and booleans are coerced to string before comparison.
  • Empty strings fail.
  • If no values are supplied (startsWith()), validation always fails.

See also