user5711656
user5711656

Reputation: 3678

how to validate form in angular 2 having one input field?

I have one input field in which user can enter email as well as 10 digit phone number if any of this condition is fulfill i want to enable continue button . I am able to enable button when user type email but I fail to enable the button when user type 10 digit mobile number

here is my code https://stackblitz.com/edit/angular-p5knn6?file=src%2Fapp%2Flogin-component%2Flogin-component.component.html

<div class="login_div">
  <form  #loginForm="ngForm">
    <input type="text" name="email"
           autocomplete="off"
           placeholder="Enter the Email or Number"
           [(ngModel)]="userenterValue"
           required
           pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$">
    <input type="submit" value="Continue"
           class="btn"
           [class.disbled]="!loginForm.valid"
           [disabled]="!loginForm.valid">
  </form>
</div>

enter image description here

enter image description here

enter image description here

Upvotes: 2

Views: 68

Answers (1)

Ashish Ranjan
Ashish Ranjan

Reputation: 12960

Just use the pattern like this:

pattern="^(\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+)$|^(\d{10})$"

Upvotes: 1

Related Questions