1

I need to validate phone number and email in the same text field using Angular JS. The form contains an input box which is allowed to type an email or phone number. If the user types email it should fire validations related to email and if its phone number it should match it with a different validation. How to achieve this?

This is how I am going to validate a single input.

<input type="text"
          class="form-control"
          id="email"
          ngModel
          name="email"
          required
          pattern="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?
^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-
z0-9])?"
          #email="ngModel">
          <div *ngIf="email.errors && (email.dirty ||  email.touched)"
  class="alert alert-danger">
            <div [hidden]="!email.errors.required">Empty Email Address</div>
          </div>
Mayur Shah
  • 3,344
  • 1
  • 22
  • 41
Harsha W
  • 3,162
  • 5
  • 43
  • 77

0 Answers0