StartsWith
The Starts With rule checks that a field's value begins with one of a given list of substrings — pass it a single prefix or several, like ['a','b'], and the field passes if it matches the start of any one of them. It's a natural fit for enforcing prefixed formats, such as a phone number that must start with a country code, an order reference that always opens with a fixed batch prefix, or a coupon code that should begin with a recognizable campaign tag. It's the mirror image of the Ends With rule, and the Includes rule is the broader version that checks for a substring anywhere in the value rather than specifically at the start.
#Directive Code Snippet
<!DOCTYPE html>
<html lang="en">
...
<form (ngSubmit)="onSubmit(form)" #form="ngForm">
<input startsWith="<ALLOWED_PREFIXES>" type="text" name="<name>" [(ngModel)]="<name>" #<name>="ngModel">
<ng-absolute-validator [formInstance]="<name>"></ng-absolute-validator>
</form>
...
</html>
#Chain Rule Code Snippet
<!DOCTYPE html>
<html lang="en">
...
<form (ngSubmit)="onSubmit(form)" #form="ngForm">
<input rule="starts_with:<ALLOWED_PREFIXES>" type="text" name="<name>" [(ngModel)]="<name>" #<name>="ngModel">
<ng-absolute-validator [formInstance]="<name>"></ng-absolute-validator>
</form>
...
</html>
#Reactive Form Code Snippet
//component.ts
export class ReactiveComponent{
public form !: FormGroup;
constructor(
private fb : FormBuilder,
private rv : ReactiveValidator,
){this.generteForm()}
generteForm(){
this.form = this.fb.group({
<name> : ['',this.rv.map('starts_with:<ALLOWED_PREFIXES>',<CUSTOM_MESSAGE>)]
})
}
}
//component.html
<!DOCTYPE html>
<html lang="en">
...
<form [formGroup]="form">
<input type="text" name="<name>" formControlName="<name>">
<ng-absolute-validator [formInstance]="form.get(<name>)"></ng-absolute-validator>
</form>
...
</html>