Form value not consoling on submit, no error found












0















In my ngForm all are ok. But I am not able to get the console value on submit. any one help me to know the issue here?



my ts file :



import { Component } from "@angular/core";
import { NgForm } from "@angular/forms";

@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
selectedUser = {
name: "",
username: "",
password: ""
};

patternName = /^[a-zA-Z ]{3,}$/;
patternUsername = /^[a-z]{3,}$/;
patternPassword = /s/;

constructor() {}

ngOnInit() {}

onSubmit(form: NgForm) {
console.log("submitted", form.value);
}
}


and the html is :



<form
class="form-horizontal"
#formSignin="ngForm"
(ngSubmit)="formSignin.valid && onSubmit(formSignin)"
>
<div class="page-header"><h2>User Sign-in</h2></div>
<div class="form-group">
<label class="control-label col-sm-2" for="name">Name</label>
<input
type="text"
class="col-sm-4"
#name="ngModel"
name="name"
id="name"
placeholder="Enter Name"
[(ngModel)]="selectedUser.name"
required
[pattern]="patternName"
/>
<div *ngIf="formSignin.submitted && !name.valid">
<span *ngIf="name.errors.required">Name must to be filled</span>
</div>
<div *ngIf="name.errors?.pattern">
Username should be min 3 charactors and alphabets only
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="username">User Name</label>
<input
type="text"
class="col-sm-4"
#username="ngModel"
name="username"
id="username"
placeholder="Enter Username"
[(ngModel)]="selectedUser.username"
required
/>
<div *ngIf="formSignin.submitted && !username.valid">
<span *ngIf="username.errors.required">Username Must filled</span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="password">Password</label>

<input
type="password"
class="col-sm-4"
#password="ngModel"
name="password"
[pattern]="patternPassword"
id="password"
placeholder="Enter Password"
[(ngModel)]="selectedUser.password"
required
/>

<div *ngIf="formSignin.submitted && !password.valid">
<span *ngIf="password.errors.required">Password required</span>
</div>
<div *ngIf="!password.errors?.required && !password.errors?.pattern">
Space not allowed
</div>
</div>
<div class="form-group d-flex">
<label class="col-sm-2"></label>
<div><button type="submit" class="btn btn-default">Submit</button></div>
</div>
</form>


Live demo : https://codesandbox.io/s/wnj9nj1l55










share|improve this question

























  • your code and validation conditions are correct only thing is password pattern is not working can you please explain that why you need that pattern

    – Soumya B. Athani
    Jan 3 at 11:03













  • @Soumya I am looking to prevent "empty" space in password

    – 3gwebtrain
    Jan 3 at 11:53
















0















In my ngForm all are ok. But I am not able to get the console value on submit. any one help me to know the issue here?



my ts file :



import { Component } from "@angular/core";
import { NgForm } from "@angular/forms";

@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
selectedUser = {
name: "",
username: "",
password: ""
};

patternName = /^[a-zA-Z ]{3,}$/;
patternUsername = /^[a-z]{3,}$/;
patternPassword = /s/;

constructor() {}

ngOnInit() {}

onSubmit(form: NgForm) {
console.log("submitted", form.value);
}
}


and the html is :



<form
class="form-horizontal"
#formSignin="ngForm"
(ngSubmit)="formSignin.valid && onSubmit(formSignin)"
>
<div class="page-header"><h2>User Sign-in</h2></div>
<div class="form-group">
<label class="control-label col-sm-2" for="name">Name</label>
<input
type="text"
class="col-sm-4"
#name="ngModel"
name="name"
id="name"
placeholder="Enter Name"
[(ngModel)]="selectedUser.name"
required
[pattern]="patternName"
/>
<div *ngIf="formSignin.submitted && !name.valid">
<span *ngIf="name.errors.required">Name must to be filled</span>
</div>
<div *ngIf="name.errors?.pattern">
Username should be min 3 charactors and alphabets only
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="username">User Name</label>
<input
type="text"
class="col-sm-4"
#username="ngModel"
name="username"
id="username"
placeholder="Enter Username"
[(ngModel)]="selectedUser.username"
required
/>
<div *ngIf="formSignin.submitted && !username.valid">
<span *ngIf="username.errors.required">Username Must filled</span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="password">Password</label>

<input
type="password"
class="col-sm-4"
#password="ngModel"
name="password"
[pattern]="patternPassword"
id="password"
placeholder="Enter Password"
[(ngModel)]="selectedUser.password"
required
/>

<div *ngIf="formSignin.submitted && !password.valid">
<span *ngIf="password.errors.required">Password required</span>
</div>
<div *ngIf="!password.errors?.required && !password.errors?.pattern">
Space not allowed
</div>
</div>
<div class="form-group d-flex">
<label class="col-sm-2"></label>
<div><button type="submit" class="btn btn-default">Submit</button></div>
</div>
</form>


Live demo : https://codesandbox.io/s/wnj9nj1l55










share|improve this question

























  • your code and validation conditions are correct only thing is password pattern is not working can you please explain that why you need that pattern

    – Soumya B. Athani
    Jan 3 at 11:03













  • @Soumya I am looking to prevent "empty" space in password

    – 3gwebtrain
    Jan 3 at 11:53














0












0








0








In my ngForm all are ok. But I am not able to get the console value on submit. any one help me to know the issue here?



my ts file :



import { Component } from "@angular/core";
import { NgForm } from "@angular/forms";

@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
selectedUser = {
name: "",
username: "",
password: ""
};

patternName = /^[a-zA-Z ]{3,}$/;
patternUsername = /^[a-z]{3,}$/;
patternPassword = /s/;

constructor() {}

ngOnInit() {}

onSubmit(form: NgForm) {
console.log("submitted", form.value);
}
}


and the html is :



<form
class="form-horizontal"
#formSignin="ngForm"
(ngSubmit)="formSignin.valid && onSubmit(formSignin)"
>
<div class="page-header"><h2>User Sign-in</h2></div>
<div class="form-group">
<label class="control-label col-sm-2" for="name">Name</label>
<input
type="text"
class="col-sm-4"
#name="ngModel"
name="name"
id="name"
placeholder="Enter Name"
[(ngModel)]="selectedUser.name"
required
[pattern]="patternName"
/>
<div *ngIf="formSignin.submitted && !name.valid">
<span *ngIf="name.errors.required">Name must to be filled</span>
</div>
<div *ngIf="name.errors?.pattern">
Username should be min 3 charactors and alphabets only
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="username">User Name</label>
<input
type="text"
class="col-sm-4"
#username="ngModel"
name="username"
id="username"
placeholder="Enter Username"
[(ngModel)]="selectedUser.username"
required
/>
<div *ngIf="formSignin.submitted && !username.valid">
<span *ngIf="username.errors.required">Username Must filled</span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="password">Password</label>

<input
type="password"
class="col-sm-4"
#password="ngModel"
name="password"
[pattern]="patternPassword"
id="password"
placeholder="Enter Password"
[(ngModel)]="selectedUser.password"
required
/>

<div *ngIf="formSignin.submitted && !password.valid">
<span *ngIf="password.errors.required">Password required</span>
</div>
<div *ngIf="!password.errors?.required && !password.errors?.pattern">
Space not allowed
</div>
</div>
<div class="form-group d-flex">
<label class="col-sm-2"></label>
<div><button type="submit" class="btn btn-default">Submit</button></div>
</div>
</form>


Live demo : https://codesandbox.io/s/wnj9nj1l55










share|improve this question
















In my ngForm all are ok. But I am not able to get the console value on submit. any one help me to know the issue here?



my ts file :



import { Component } from "@angular/core";
import { NgForm } from "@angular/forms";

@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
selectedUser = {
name: "",
username: "",
password: ""
};

patternName = /^[a-zA-Z ]{3,}$/;
patternUsername = /^[a-z]{3,}$/;
patternPassword = /s/;

constructor() {}

ngOnInit() {}

onSubmit(form: NgForm) {
console.log("submitted", form.value);
}
}


and the html is :



<form
class="form-horizontal"
#formSignin="ngForm"
(ngSubmit)="formSignin.valid && onSubmit(formSignin)"
>
<div class="page-header"><h2>User Sign-in</h2></div>
<div class="form-group">
<label class="control-label col-sm-2" for="name">Name</label>
<input
type="text"
class="col-sm-4"
#name="ngModel"
name="name"
id="name"
placeholder="Enter Name"
[(ngModel)]="selectedUser.name"
required
[pattern]="patternName"
/>
<div *ngIf="formSignin.submitted && !name.valid">
<span *ngIf="name.errors.required">Name must to be filled</span>
</div>
<div *ngIf="name.errors?.pattern">
Username should be min 3 charactors and alphabets only
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="username">User Name</label>
<input
type="text"
class="col-sm-4"
#username="ngModel"
name="username"
id="username"
placeholder="Enter Username"
[(ngModel)]="selectedUser.username"
required
/>
<div *ngIf="formSignin.submitted && !username.valid">
<span *ngIf="username.errors.required">Username Must filled</span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="password">Password</label>

<input
type="password"
class="col-sm-4"
#password="ngModel"
name="password"
[pattern]="patternPassword"
id="password"
placeholder="Enter Password"
[(ngModel)]="selectedUser.password"
required
/>

<div *ngIf="formSignin.submitted && !password.valid">
<span *ngIf="password.errors.required">Password required</span>
</div>
<div *ngIf="!password.errors?.required && !password.errors?.pattern">
Space not allowed
</div>
</div>
<div class="form-group d-flex">
<label class="col-sm-2"></label>
<div><button type="submit" class="btn btn-default">Submit</button></div>
</div>
</form>


Live demo : https://codesandbox.io/s/wnj9nj1l55







angular angular-forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 13:46









Aju John

1,7581619




1,7581619










asked Jan 3 at 9:52









user2024080user2024080

1,61342439




1,61342439













  • your code and validation conditions are correct only thing is password pattern is not working can you please explain that why you need that pattern

    – Soumya B. Athani
    Jan 3 at 11:03













  • @Soumya I am looking to prevent "empty" space in password

    – 3gwebtrain
    Jan 3 at 11:53



















  • your code and validation conditions are correct only thing is password pattern is not working can you please explain that why you need that pattern

    – Soumya B. Athani
    Jan 3 at 11:03













  • @Soumya I am looking to prevent "empty" space in password

    – 3gwebtrain
    Jan 3 at 11:53

















your code and validation conditions are correct only thing is password pattern is not working can you please explain that why you need that pattern

– Soumya B. Athani
Jan 3 at 11:03







your code and validation conditions are correct only thing is password pattern is not working can you please explain that why you need that pattern

– Soumya B. Athani
Jan 3 at 11:03















@Soumya I am looking to prevent "empty" space in password

– 3gwebtrain
Jan 3 at 11:53





@Soumya I am looking to prevent "empty" space in password

– 3gwebtrain
Jan 3 at 11:53












2 Answers
2






active

oldest

votes


















1














You didn't put the ngSubmit right..



The right way to do is to only assigned a function and in the function check if the form is valid !



onSubmit(form: NgForm) {
if (form.valid) {
console.log("submitted", form.value);
} else {
console.log("form not valid !");
}


}



https://codesandbox.io/s/0xvky6woz0



EDIT

Your RegEx for the password is invalid, so even if the name and the username is valid, the password is not.. I don't know what you are looking for but something like this will work: /^[a-z]+/i


EDIT 2

And in case you want a password with everything except space: /^[a-zA-Z_-]+$/

Also, you had an error in your html file:



 <div *ngIf="!password.errors?.required && password.errors?.pattern">
Space not allowed
</div>





share|improve this answer


























  • My question is, when the form is correct, I am not getting a word as "submitted" -

    – 3gwebtrain
    Jan 3 at 10:31











  • i edit my answer @3gwebtrain and also the codesandbox, now, it's working !

    – B.Benjo
    Jan 3 at 10:38





















0














this is the working example you can try this
https://codesandbox.io/s/lyqx80n32z



here i used password pattern it will not allow you to enter space in the password field




patternPassword = /^S{0,50}$/;




and modified your error message condition



 <div *ngIf="!password.errors?.required && password.errors?.pattern">
Space not allowed
</div>





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%2f54019841%2fform-value-not-consoling-on-submit-no-error-found%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









    1














    You didn't put the ngSubmit right..



    The right way to do is to only assigned a function and in the function check if the form is valid !



    onSubmit(form: NgForm) {
    if (form.valid) {
    console.log("submitted", form.value);
    } else {
    console.log("form not valid !");
    }


    }



    https://codesandbox.io/s/0xvky6woz0



    EDIT

    Your RegEx for the password is invalid, so even if the name and the username is valid, the password is not.. I don't know what you are looking for but something like this will work: /^[a-z]+/i


    EDIT 2

    And in case you want a password with everything except space: /^[a-zA-Z_-]+$/

    Also, you had an error in your html file:



     <div *ngIf="!password.errors?.required && password.errors?.pattern">
    Space not allowed
    </div>





    share|improve this answer


























    • My question is, when the form is correct, I am not getting a word as "submitted" -

      – 3gwebtrain
      Jan 3 at 10:31











    • i edit my answer @3gwebtrain and also the codesandbox, now, it's working !

      – B.Benjo
      Jan 3 at 10:38


















    1














    You didn't put the ngSubmit right..



    The right way to do is to only assigned a function and in the function check if the form is valid !



    onSubmit(form: NgForm) {
    if (form.valid) {
    console.log("submitted", form.value);
    } else {
    console.log("form not valid !");
    }


    }



    https://codesandbox.io/s/0xvky6woz0



    EDIT

    Your RegEx for the password is invalid, so even if the name and the username is valid, the password is not.. I don't know what you are looking for but something like this will work: /^[a-z]+/i


    EDIT 2

    And in case you want a password with everything except space: /^[a-zA-Z_-]+$/

    Also, you had an error in your html file:



     <div *ngIf="!password.errors?.required && password.errors?.pattern">
    Space not allowed
    </div>





    share|improve this answer


























    • My question is, when the form is correct, I am not getting a word as "submitted" -

      – 3gwebtrain
      Jan 3 at 10:31











    • i edit my answer @3gwebtrain and also the codesandbox, now, it's working !

      – B.Benjo
      Jan 3 at 10:38
















    1












    1








    1







    You didn't put the ngSubmit right..



    The right way to do is to only assigned a function and in the function check if the form is valid !



    onSubmit(form: NgForm) {
    if (form.valid) {
    console.log("submitted", form.value);
    } else {
    console.log("form not valid !");
    }


    }



    https://codesandbox.io/s/0xvky6woz0



    EDIT

    Your RegEx for the password is invalid, so even if the name and the username is valid, the password is not.. I don't know what you are looking for but something like this will work: /^[a-z]+/i


    EDIT 2

    And in case you want a password with everything except space: /^[a-zA-Z_-]+$/

    Also, you had an error in your html file:



     <div *ngIf="!password.errors?.required && password.errors?.pattern">
    Space not allowed
    </div>





    share|improve this answer















    You didn't put the ngSubmit right..



    The right way to do is to only assigned a function and in the function check if the form is valid !



    onSubmit(form: NgForm) {
    if (form.valid) {
    console.log("submitted", form.value);
    } else {
    console.log("form not valid !");
    }


    }



    https://codesandbox.io/s/0xvky6woz0



    EDIT

    Your RegEx for the password is invalid, so even if the name and the username is valid, the password is not.. I don't know what you are looking for but something like this will work: /^[a-z]+/i


    EDIT 2

    And in case you want a password with everything except space: /^[a-zA-Z_-]+$/

    Also, you had an error in your html file:



     <div *ngIf="!password.errors?.required && password.errors?.pattern">
    Space not allowed
    </div>






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 3 at 10:43

























    answered Jan 3 at 10:26









    B.BenjoB.Benjo

    413311




    413311













    • My question is, when the form is correct, I am not getting a word as "submitted" -

      – 3gwebtrain
      Jan 3 at 10:31











    • i edit my answer @3gwebtrain and also the codesandbox, now, it's working !

      – B.Benjo
      Jan 3 at 10:38





















    • My question is, when the form is correct, I am not getting a word as "submitted" -

      – 3gwebtrain
      Jan 3 at 10:31











    • i edit my answer @3gwebtrain and also the codesandbox, now, it's working !

      – B.Benjo
      Jan 3 at 10:38



















    My question is, when the form is correct, I am not getting a word as "submitted" -

    – 3gwebtrain
    Jan 3 at 10:31





    My question is, when the form is correct, I am not getting a word as "submitted" -

    – 3gwebtrain
    Jan 3 at 10:31













    i edit my answer @3gwebtrain and also the codesandbox, now, it's working !

    – B.Benjo
    Jan 3 at 10:38







    i edit my answer @3gwebtrain and also the codesandbox, now, it's working !

    – B.Benjo
    Jan 3 at 10:38















    0














    this is the working example you can try this
    https://codesandbox.io/s/lyqx80n32z



    here i used password pattern it will not allow you to enter space in the password field




    patternPassword = /^S{0,50}$/;




    and modified your error message condition



     <div *ngIf="!password.errors?.required && password.errors?.pattern">
    Space not allowed
    </div>





    share|improve this answer






























      0














      this is the working example you can try this
      https://codesandbox.io/s/lyqx80n32z



      here i used password pattern it will not allow you to enter space in the password field




      patternPassword = /^S{0,50}$/;




      and modified your error message condition



       <div *ngIf="!password.errors?.required && password.errors?.pattern">
      Space not allowed
      </div>





      share|improve this answer




























        0












        0








        0







        this is the working example you can try this
        https://codesandbox.io/s/lyqx80n32z



        here i used password pattern it will not allow you to enter space in the password field




        patternPassword = /^S{0,50}$/;




        and modified your error message condition



         <div *ngIf="!password.errors?.required && password.errors?.pattern">
        Space not allowed
        </div>





        share|improve this answer















        this is the working example you can try this
        https://codesandbox.io/s/lyqx80n32z



        here i used password pattern it will not allow you to enter space in the password field




        patternPassword = /^S{0,50}$/;




        and modified your error message condition



         <div *ngIf="!password.errors?.required && password.errors?.pattern">
        Space not allowed
        </div>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 3 at 12:30

























        answered Jan 3 at 10:27









        Soumya B. AthaniSoumya B. Athani

        3331312




        3331312






























            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%2f54019841%2fform-value-not-consoling-on-submit-no-error-found%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







            Popular posts from this blog

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas

            Can't read property showImagePicker of undefined in react native iOS