1

Here I have one input field that I want to make mandatory url input field and I done this code.when I put url in input field or put any other text only input field in both case it saves input field and save in database. so, how to make mandatory url type input field in angular 6.

<form method="post" enctype="multipart/form-data">
<mat-form-field>
  <input matInput type="url" pattern="https?://.+" placeholder="Large Icon" name="largeicon" #largeicon="ngModel"
           [(ngModel)]="notificationObj.largeicon" required>
</mat-form-field>

<div *ngIf="largeicon.errors && (largeicon.dirty || largeicon.touched)" class="alert alert-danger">
  <div [hidden]="!largeicon.errors.required">URL is required!</div>
  <div [hidden]="!largeicon.errors.pattern">Must be a valid URL!</div>
</div>
  <button mat-raised-button (click)="sendNotification()">SEND NOTIFICATION</button>  

</form>
1

4 Answers 4

1

On Button click "sendNotification()" you are not checking the validation. There are many ways to block the method call on validation. Find below sample code.

  <button mat-raised-button [disabled]="largeicon.errors && (largeicon.errors.required || largeicon.errors.pattern)" (click)="sendNotification()">SEND NOTIFICATION</button>  

Above code will disable button when validation failed.

Sign up to request clarification or add additional context in comments.

Comments

1

your pattern should be

pattern="/^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/"

or else on form submit function to check the regex value if matched then true else false

this fiddle will help you to understand better

1 Comment

You don't need ` inside of character classes unless it's to escape [` or ], and {0,1} is more concisely written as ?. Also, the TLD regex is wrong; it will reject .co.uk domains, for instance.
0

You have done some mandatory things wrong to work with ngForm in the way as it is designed.

Please take a look at this guide: https://www.concretepage.com/angular-2/angular-2-ngform-with-ngmodel-directive-example

These are the major keys:

  • Use ngForm at the form tag
  • use ngSubmit instead of an (click) button, because that bypasses the form

Comments

0

try to add disabled in your form submit button and your already using the required in input field so,it will work now

 <form method="post" enctype="multipart/form-data" #ngForm="formName">
    <mat-form-field>
      <input matInput type="url" pattern="https?://.+" placeholder="Large Icon" name="largeicon" #largeicon="ngModel"
               [(ngModel)]="notificationObj.largeicon" required>
    </mat-form-field>

    <div *ngIf="largeicon.errors && (largeicon.dirty || largeicon.touched)" class="alert alert-danger">
      <div [hidden]="!largeicon.errors.required">URL is required!</div>
      <div [hidden]="!largeicon.errors.pattern">Must be a valid URL!</div>
    </div>
      <button mat-raised-button (click)="sendNotification()" [disabled]="!formName.form.valid">SEND NOTIFICATION</button>  

    </form>

for more details look here in angular docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.