Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

angular validation on numbers

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  registerForm: FormGroup;
  submitted = false;
  constructor(private formBuilder: FormBuilder) { }
  //only number will be add
  keyPress(event: any) {
    const pattern = /[0-9+- ]/;
    let inputChar = String.fromCharCode(event.charCode);
    if (event.keyCode != 8 && !pattern.test(inputChar)) {
      event.preventDefault();
    }
  }
  ngOnInit() {
    this.registerForm = this.formBuilder.group({
      phonenumber: ['', [ Validators.required,
        Validators.pattern("^[0-9]*$"),
        Validators.minLength(10), Validators.maxLength(10)]]
    });
}
// convenience getter for easy access to form fields
get f() { return this.registerForm.controls; }
onSubmit() {
  this.submitted = true;
  // stop here if form is invalid
  if (this.registerForm.invalid) {
    return;
  }

}

}
Comment

angular validation on numbers

form [formGroup]="registerForm" (ngSubmit)="onSubmit()"
div class="col-md-4"
  div class="form-group"
    label for="">YOUR PHONE NUMBER /label
    input (keypress)="keyPress($event)" required type="text" formControlName="phonenumber" class="form-control" placeholder="Enter Your phone Number" [ngClass]="{ 'is-invalid': submitted && f.phonenumber.errors }"
    div ngIf="submitted && f.phonenumber.errors" class="invalid-feedback"
    div ngIf="f.phonenumber.errors.required">Phone number is required /div
    div *ngIf="f.phonenumber.errors.pattern || f.phonenumber.errors.maxlength || f.phonenumber.errors.minlength">Phone number must be at least 10 numbers /div
    /div
    /div
  /div
input type="submit" class="mw-ui-btn" value="Submit"
/form
Comment

angular number validation

ng-pattern="re"
Comment

PREVIOUS NEXT
Code Example
Javascript :: js functional ajax requests 
Javascript :: vue js laravel tutorial 
Javascript :: parse query url javascript 
Javascript :: dayjs subtract days 
Javascript :: how to do subtraction in javascript 
Javascript :: vanilla js 
Javascript :: how to make a delete button in javascript 
Javascript :: python json replace string 
Javascript :: turn string into number javascript 
Javascript :: remove array from array javascript 
Javascript :: discord.js purge 
Javascript :: adding pre tag javascript 
Javascript :: disable js in chrome dev tools 
Javascript :: javascript get all elements by class starting with 
Javascript :: javascrip functions parameters 
Javascript :: add role to channel discord.js 
Javascript :: Adding an item to an array 
Javascript :: reference data types in javascript 
Javascript :: upload file in node 
Javascript :: serializes to the same string 
Javascript :: python best practices example 
Javascript :: javascript first or default 
Javascript :: discord js people in voice channel 
Javascript :: insertbefore javascript 
Javascript :: never give up 
Javascript :: creating a custom function to use nodemailer to send email 
Javascript :: ucwords javascript 
Javascript :: circle collision javascript 
Javascript :: node js find directory change directory 
Python :: francais a anglais 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =