Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
ng2-validation
Advanced tools
Angular2 custom validation, inspired by jQuery validation.
npm install ng2-validation --save
import FormsModule
and CustomFormsModule
in app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { CustomFormsModule } from 'ng2-validation'
import { AppComponent } from './app.component';
@NgModule({
imports: [BrowserModule, FormsModule, CustomFormsModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {
}
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" [rangeLength]="[5, 9]"/>
<p *ngIf="field.errors?.rangeLength">error message</p>
<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" [min]="10"/>
<p *ngIf="field.errors?.min">error message</p>
<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" [max]="20"/>
<p *ngIf="field.errors?.max">error message</p>
<input type="number" [(ngModel)]="model.field" name="field" #field="ngModel" [range]="[10, 20]"/>
<p *ngIf="field.errors?.range">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" digits/>
<p *ngIf="field.errors?.digits">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" number/>
<p *ngIf="field.errors?.number">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" url/>
<p *ngIf="field.errors?.url">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" email/>
<p *ngIf="field.errors?.email">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" date/>
<p *ngIf="field.errors?.date">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" dateISO/>
<p *ngIf="field.errors?.dateISO">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" creditCard/>
<p *ngIf="field.errors?.creditCard">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" json/>
<p *ngIf="field.errors?.json">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" base64/>
<p *ngIf="field.errors?.base64">error message</p>
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" [phone]="'zh-CN'"/>
<p *ngIf="field.errors?.phone">error message</p>
default: en-US
support
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" [uuid]="'all'"/>
<p *ngIf="field.errors?.uuid">error message</p>
default: all
support
<input type="text" [(ngModel)]="model.field" name="field" #field="ngModel" [equal]="'xxx'"/>
<p *ngIf="field.errors?.equal">error message</p>
<form #demoForm="ngForm" novalidate>
<div ngModelGroup="passwordGroup" equalTo>
<input type="password" ngModel name="password" #password="ngModel" required/>
<p *ngIf="password?.errors?.required">required error</p>
<input type="password" ngModel name="certainPassword"/>
<p *ngIf="demoForm.form.controls.passwordGroup?.errors?.equalTo">equalTo error</p>
</div>
</form>
import ReactiveFormsModule
in app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
imports: [BrowserModule, ReactiveFormsModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule {
}
import CustomValidators
in app.component.ts
import { Component } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { CustomValidators } from 'ng2-validation';
@Component({
selector: 'app',
template: require('./app.html')
})
export class AppComponent {
form: FormGroup;
constructor() {
this.form = new FormGroup({
field: new FormControl('', CustomValidators.range([5, 9]))
});
}
}
<input type="text" formControlName="field"/>
<p *ngIf="demoForm.from.controls.field.errors?.rangeLength">error message</p>
new FormControl('', CustomValidators.rangeLength([5, 9]))
new FormControl('', CustomValidators.min(10))
new FormControl('', CustomValidators.max(20))
new FormControl('', CustomValidators.range([10, 20]))
new FormControl('', CustomValidators.digits)
new FormControl('', CustomValidators.number)
new FormControl('', CustomValidators.url)
new FormControl('', CustomValidators.email)
new FormControl('', CustomValidators.date)
new FormControl('', CustomValidators.dateISO)
new FormControl('', CustomValidators.creditCard)
new FormControl('', CustomValidators.json)
new FormControl('', CustomValidators.base64)
new FormControl('', CustomValidators.phone('zh-CN'))
new FormControl('', CustomValidators.uuid('3'))
new FormControl('', CustomValidators.equal('xxx'))
@Component({
selector: 'app',
template: require('./app.html')
})
export class AppComponent implements OnInit {
form: FormGroup;
ngOnInit() {
var password = new FormControl('', Validators.required);
var certainPassword = new FormControl('');
this.form = new FormGroup({
passwordGroup: new FormGroup({
password: password,
certainPassword: certainPassword
}, CustomValidators.equalTo)
});
}
}
<form [formGroup]="form" novalidate>
<div formGroupName="passwordGroup">
<input type="password" formControlName="password"/>
<p *ngIf="form.controls.passwordGroup.controls.password?.errors?.required">required error</p>
<input type="password" formControlName="certainPassword"/>
<p *ngIf="form.controls.passwordGroup?.errors?.equalTo">equalTo error</p>
</div>
<button>submit</button>
</form>
MIT
FAQs
angular2 validation
The npm package ng2-validation receives a total of 8,897 weekly downloads. As such, ng2-validation popularity was classified as popular.
We found that ng2-validation demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.