Size
The Size rule caps an uploaded file's weight, rejecting anything larger than the given limit in kilobytes — size:120 rejects any file over 120 KB. It's a straightforward guard against oversized uploads, useful for keeping a profile photo lightweight, capping a résumé or document attachment so it doesn't strain your storage or bandwidth, or enforcing a platform's own upload ceiling before the file ever reaches your server. Pair it with the Image, Mimes, or Dimension rules when a field also needs to constrain the file's type or pixel dimensions alongside its size.
#Directive Code Snippet
<!DOCTYPE html>
<html lang="en">
...
<form (ngSubmit)="onSubmit(form)" #form="ngForm">
<input [size]="<FILE_SIZE_IN_KB>" type="file" 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="size:<FILE_SIZE_IN_KB>" type="file" 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('size:<FILE_SIZE_IN_KB>',<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>