Fire validation when focus out from input in angular?










2















Email validation is being fired as we keep typing in textbox. I want this validation to be fired when user focuses out of the textbox



Below is my code:



<input class="form-control" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]2,3$"
name="Email" type="email" [(ngModel)]="model.Email" #Email="ngModel" required>

<div class="red" *ngIf="Email.errors && (Email.dirty || Email.touched)">
<div [hidden]="!Email.errors.pattern">
Please enter a valid email.
</div>
</div>


Please suggest me how can I achieve this.
Thanks in advance.










share|improve this question






















  • do you also want to remove validation while typing?

    – Sumit Agarwal
    Sep 19 '17 at 10:03











  • I see that your are using, model driven form here. Try using reactive form module. There you can call validation manually on blur or focus.

    – Sumit Agarwal
    Sep 19 '17 at 10:08











  • Template driven forum is used in my whole application . is reactive form module is only way of achieve this?

    – Umang Patwa
    Sep 19 '17 at 10:12











  • yes I also want to remove validation while typing

    – Umang Patwa
    Sep 19 '17 at 10:14











  • going by the link - github.com/angular/angular/issues/7113 provided by @Sam in answer below, it seems all you can do is to handle when you show the message and not the validation call (especially with Template Driven Form)

    – Sumit Agarwal
    Sep 19 '17 at 10:15















2















Email validation is being fired as we keep typing in textbox. I want this validation to be fired when user focuses out of the textbox



Below is my code:



<input class="form-control" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]2,3$"
name="Email" type="email" [(ngModel)]="model.Email" #Email="ngModel" required>

<div class="red" *ngIf="Email.errors && (Email.dirty || Email.touched)">
<div [hidden]="!Email.errors.pattern">
Please enter a valid email.
</div>
</div>


Please suggest me how can I achieve this.
Thanks in advance.










share|improve this question






















  • do you also want to remove validation while typing?

    – Sumit Agarwal
    Sep 19 '17 at 10:03











  • I see that your are using, model driven form here. Try using reactive form module. There you can call validation manually on blur or focus.

    – Sumit Agarwal
    Sep 19 '17 at 10:08











  • Template driven forum is used in my whole application . is reactive form module is only way of achieve this?

    – Umang Patwa
    Sep 19 '17 at 10:12











  • yes I also want to remove validation while typing

    – Umang Patwa
    Sep 19 '17 at 10:14











  • going by the link - github.com/angular/angular/issues/7113 provided by @Sam in answer below, it seems all you can do is to handle when you show the message and not the validation call (especially with Template Driven Form)

    – Sumit Agarwal
    Sep 19 '17 at 10:15













2












2








2


1






Email validation is being fired as we keep typing in textbox. I want this validation to be fired when user focuses out of the textbox



Below is my code:



<input class="form-control" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]2,3$"
name="Email" type="email" [(ngModel)]="model.Email" #Email="ngModel" required>

<div class="red" *ngIf="Email.errors && (Email.dirty || Email.touched)">
<div [hidden]="!Email.errors.pattern">
Please enter a valid email.
</div>
</div>


Please suggest me how can I achieve this.
Thanks in advance.










share|improve this question














Email validation is being fired as we keep typing in textbox. I want this validation to be fired when user focuses out of the textbox



Below is my code:



<input class="form-control" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]2,3$"
name="Email" type="email" [(ngModel)]="model.Email" #Email="ngModel" required>

<div class="red" *ngIf="Email.errors && (Email.dirty || Email.touched)">
<div [hidden]="!Email.errors.pattern">
Please enter a valid email.
</div>
</div>


Please suggest me how can I achieve this.
Thanks in advance.







angular angular-validation angular4-forms






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 19 '17 at 9:45









Umang PatwaUmang Patwa

1,04611528




1,04611528












  • do you also want to remove validation while typing?

    – Sumit Agarwal
    Sep 19 '17 at 10:03











  • I see that your are using, model driven form here. Try using reactive form module. There you can call validation manually on blur or focus.

    – Sumit Agarwal
    Sep 19 '17 at 10:08











  • Template driven forum is used in my whole application . is reactive form module is only way of achieve this?

    – Umang Patwa
    Sep 19 '17 at 10:12











  • yes I also want to remove validation while typing

    – Umang Patwa
    Sep 19 '17 at 10:14











  • going by the link - github.com/angular/angular/issues/7113 provided by @Sam in answer below, it seems all you can do is to handle when you show the message and not the validation call (especially with Template Driven Form)

    – Sumit Agarwal
    Sep 19 '17 at 10:15

















  • do you also want to remove validation while typing?

    – Sumit Agarwal
    Sep 19 '17 at 10:03











  • I see that your are using, model driven form here. Try using reactive form module. There you can call validation manually on blur or focus.

    – Sumit Agarwal
    Sep 19 '17 at 10:08











  • Template driven forum is used in my whole application . is reactive form module is only way of achieve this?

    – Umang Patwa
    Sep 19 '17 at 10:12











  • yes I also want to remove validation while typing

    – Umang Patwa
    Sep 19 '17 at 10:14











  • going by the link - github.com/angular/angular/issues/7113 provided by @Sam in answer below, it seems all you can do is to handle when you show the message and not the validation call (especially with Template Driven Form)

    – Sumit Agarwal
    Sep 19 '17 at 10:15
















do you also want to remove validation while typing?

– Sumit Agarwal
Sep 19 '17 at 10:03





do you also want to remove validation while typing?

– Sumit Agarwal
Sep 19 '17 at 10:03













I see that your are using, model driven form here. Try using reactive form module. There you can call validation manually on blur or focus.

– Sumit Agarwal
Sep 19 '17 at 10:08





I see that your are using, model driven form here. Try using reactive form module. There you can call validation manually on blur or focus.

– Sumit Agarwal
Sep 19 '17 at 10:08













Template driven forum is used in my whole application . is reactive form module is only way of achieve this?

– Umang Patwa
Sep 19 '17 at 10:12





Template driven forum is used in my whole application . is reactive form module is only way of achieve this?

– Umang Patwa
Sep 19 '17 at 10:12













yes I also want to remove validation while typing

– Umang Patwa
Sep 19 '17 at 10:14





yes I also want to remove validation while typing

– Umang Patwa
Sep 19 '17 at 10:14













going by the link - github.com/angular/angular/issues/7113 provided by @Sam in answer below, it seems all you can do is to handle when you show the message and not the validation call (especially with Template Driven Form)

– Sumit Agarwal
Sep 19 '17 at 10:15





going by the link - github.com/angular/angular/issues/7113 provided by @Sam in answer below, it seems all you can do is to handle when you show the message and not the validation call (especially with Template Driven Form)

– Sumit Agarwal
Sep 19 '17 at 10:15












2 Answers
2






active

oldest

votes


















0














This will be possible in version 5, which is not released yet.



See: https://github.com/angular/angular/issues/7113



In the meantime:



<input class="form-control" formControlName="userName" placeholder="User Name" type="text" (blur)="checkUserExists()"/>
<div class="alert-danger" *ngIf="userName.invalid && userName.touched">
<div *ngIf="userName.hasError('required')">User Name is required</div>
<div *ngIf="userName.hasError('userExists')">userName.errors.userExists</div>
</div>

checkUserExists()
if (this.userName.value)
this.regServ.userNameExists(this.userName.value)
.subscribe((exists) =>
if (exists)
this.userName.setErrors( userExists: `User Name "$this.userName.value" already exists` );

);







share|improve this answer























  • hey sam is this feature available yet in angular 5/6.? if yes please can you edit the answer and show how to do it.

    – Saif
    Aug 2 '18 at 18:11


















0














Starting with the version 6+ form field validation can be set via updateOn property.



With template-driven forms:



<input [(ngModel)]="name" [ngModelOptions]="updateOn: 'blur'">


With reactive forms:



new FormControl('', updateOn: 'blur');


And if you have validators set:



new FormControl('', validators: [Validators.required, Validators.email], updateOn: 'blur')



Source: https://angular.io/guide/form-validation#note-on-performance







share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46297130%2ffire-validation-when-focus-out-from-input-in-angular%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This will be possible in version 5, which is not released yet.



    See: https://github.com/angular/angular/issues/7113



    In the meantime:



    <input class="form-control" formControlName="userName" placeholder="User Name" type="text" (blur)="checkUserExists()"/>
    <div class="alert-danger" *ngIf="userName.invalid && userName.touched">
    <div *ngIf="userName.hasError('required')">User Name is required</div>
    <div *ngIf="userName.hasError('userExists')">userName.errors.userExists</div>
    </div>

    checkUserExists()
    if (this.userName.value)
    this.regServ.userNameExists(this.userName.value)
    .subscribe((exists) =>
    if (exists)
    this.userName.setErrors( userExists: `User Name "$this.userName.value" already exists` );

    );







    share|improve this answer























    • hey sam is this feature available yet in angular 5/6.? if yes please can you edit the answer and show how to do it.

      – Saif
      Aug 2 '18 at 18:11















    0














    This will be possible in version 5, which is not released yet.



    See: https://github.com/angular/angular/issues/7113



    In the meantime:



    <input class="form-control" formControlName="userName" placeholder="User Name" type="text" (blur)="checkUserExists()"/>
    <div class="alert-danger" *ngIf="userName.invalid && userName.touched">
    <div *ngIf="userName.hasError('required')">User Name is required</div>
    <div *ngIf="userName.hasError('userExists')">userName.errors.userExists</div>
    </div>

    checkUserExists()
    if (this.userName.value)
    this.regServ.userNameExists(this.userName.value)
    .subscribe((exists) =>
    if (exists)
    this.userName.setErrors( userExists: `User Name "$this.userName.value" already exists` );

    );







    share|improve this answer























    • hey sam is this feature available yet in angular 5/6.? if yes please can you edit the answer and show how to do it.

      – Saif
      Aug 2 '18 at 18:11













    0












    0








    0







    This will be possible in version 5, which is not released yet.



    See: https://github.com/angular/angular/issues/7113



    In the meantime:



    <input class="form-control" formControlName="userName" placeholder="User Name" type="text" (blur)="checkUserExists()"/>
    <div class="alert-danger" *ngIf="userName.invalid && userName.touched">
    <div *ngIf="userName.hasError('required')">User Name is required</div>
    <div *ngIf="userName.hasError('userExists')">userName.errors.userExists</div>
    </div>

    checkUserExists()
    if (this.userName.value)
    this.regServ.userNameExists(this.userName.value)
    .subscribe((exists) =>
    if (exists)
    this.userName.setErrors( userExists: `User Name "$this.userName.value" already exists` );

    );







    share|improve this answer













    This will be possible in version 5, which is not released yet.



    See: https://github.com/angular/angular/issues/7113



    In the meantime:



    <input class="form-control" formControlName="userName" placeholder="User Name" type="text" (blur)="checkUserExists()"/>
    <div class="alert-danger" *ngIf="userName.invalid && userName.touched">
    <div *ngIf="userName.hasError('required')">User Name is required</div>
    <div *ngIf="userName.hasError('userExists')">userName.errors.userExists</div>
    </div>

    checkUserExists()
    if (this.userName.value)
    this.regServ.userNameExists(this.userName.value)
    .subscribe((exists) =>
    if (exists)
    this.userName.setErrors( userExists: `User Name "$this.userName.value" already exists` );

    );








    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Sep 19 '17 at 10:06









    SamSam

    2,24121839




    2,24121839












    • hey sam is this feature available yet in angular 5/6.? if yes please can you edit the answer and show how to do it.

      – Saif
      Aug 2 '18 at 18:11

















    • hey sam is this feature available yet in angular 5/6.? if yes please can you edit the answer and show how to do it.

      – Saif
      Aug 2 '18 at 18:11
















    hey sam is this feature available yet in angular 5/6.? if yes please can you edit the answer and show how to do it.

    – Saif
    Aug 2 '18 at 18:11





    hey sam is this feature available yet in angular 5/6.? if yes please can you edit the answer and show how to do it.

    – Saif
    Aug 2 '18 at 18:11













    0














    Starting with the version 6+ form field validation can be set via updateOn property.



    With template-driven forms:



    <input [(ngModel)]="name" [ngModelOptions]="updateOn: 'blur'">


    With reactive forms:



    new FormControl('', updateOn: 'blur');


    And if you have validators set:



    new FormControl('', validators: [Validators.required, Validators.email], updateOn: 'blur')



    Source: https://angular.io/guide/form-validation#note-on-performance







    share|improve this answer



























      0














      Starting with the version 6+ form field validation can be set via updateOn property.



      With template-driven forms:



      <input [(ngModel)]="name" [ngModelOptions]="updateOn: 'blur'">


      With reactive forms:



      new FormControl('', updateOn: 'blur');


      And if you have validators set:



      new FormControl('', validators: [Validators.required, Validators.email], updateOn: 'blur')



      Source: https://angular.io/guide/form-validation#note-on-performance







      share|improve this answer

























        0












        0








        0







        Starting with the version 6+ form field validation can be set via updateOn property.



        With template-driven forms:



        <input [(ngModel)]="name" [ngModelOptions]="updateOn: 'blur'">


        With reactive forms:



        new FormControl('', updateOn: 'blur');


        And if you have validators set:



        new FormControl('', validators: [Validators.required, Validators.email], updateOn: 'blur')



        Source: https://angular.io/guide/form-validation#note-on-performance







        share|improve this answer













        Starting with the version 6+ form field validation can be set via updateOn property.



        With template-driven forms:



        <input [(ngModel)]="name" [ngModelOptions]="updateOn: 'blur'">


        With reactive forms:



        new FormControl('', updateOn: 'blur');


        And if you have validators set:



        new FormControl('', validators: [Validators.required, Validators.email], updateOn: 'blur')



        Source: https://angular.io/guide/form-validation#note-on-performance








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 23:23









        mik-tmik-t

        2,76842451




        2,76842451



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46297130%2ffire-validation-when-focus-out-from-input-in-angular%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3