3

In my form I check the name and email validation with name

<input matInputrequired 
       minlength="3" 
       placeholder="Name" 
       ngModel   
       name="name" 
       #firstName="ngModel"
       [(ngModel)]="apiResult.name" 
       id="firstName">

And email with

<input matInput 
       placeholder="Email" 
       ngModel name="email" 
       maxlength="100" 
       #email="ngModel" 
       email 
       [(ngModel)]="apiResult.email" 
       id="email">

if the name field is empty and less than 3 characters. email is not required but if user added an email it should in email format. then if these are success only enable the submit button.

4 Answers 4

13

What is your question?

If you want to see an example of building a simple form with validation check out the movie-edit.component.html file in this repo: https://github.com/DeborahK/MovieHunter

It contains code to disable the submit/save button like this:

<button class="btn btn-primary" type="submit" [disabled]='!editForm.valid'>
    Save
</button>
Sign up to request clarification or add additional context in comments.

Comments

11

Okay we can use template driven form:

<form #myForm="ngForm">
   Your email and username inputs

   <button type="submit" [disabled]="!myForm.valid">Submit</button>
</form>

4 Comments

but the matter is when it is empty the email field also get validation error I want to check only email is not empty then it should validate. because email is not required.
(name.valid AND (if(email is not emty) then(email is valid))) something like this
Email input field should not show empty validation error unless you add required validation, also the form is valid only if all it's control is valid. So, if you want the email to be checked only if you required it.
I want to check the email in email format if user entered an email so I used html email field for input. then form.valid get an error with email empty
3

Use [disabled]="myForm.invalid":

<button [disabled]="myForm.invalid" type="submit">Submit</button>

Comments

-1

I think you are looking for this:

<button [disabled]="!myForm.form.valid" class="btn btn-primary" type="submit">Submit</button>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.