简体   繁体   中英

How do you capture the date and time when a user clicks the submit button? Angular 2

I have an html file with an onRegisterSubmit() function to submit a form. My register.ts file looks like this... it adds the user from the submitted form in html and validates and authenticates the user.

    onRegisterSubmit(){

    const user = {
        name: this.name,
        username: this.username,
        email: this.email,
        password: this.password
    }

    //calling the validate service validate register function
    //If the forms are not all filled
    if(!this.validateService.validateRegister(user)){

        //using flash messages
        this.flashMessage.show('Please fill in all fields', {cssClass: 
        'alert-danger', timeout: 3000});

        return false;

    }

  //validate email
    if(!this.validateService.validateEmail(user.email)){

        this.flashMessage.show('Please enter a valid email', {cssClass: 
        'alert-danger', timeout: 3000});

        return false;

    }

  //register User/ talks to authservice/ Since its an observable, we have to 
  //subscribe to it
  //Data is the return info received in users.js
  this.authService.registerUser(user).subscribe(data => {

     //Receive a true or false based on the add user function in users.js 
     if(data.success){

       this.flashMessage.show('You are now Registered', {cssClass: 'alert-
        success', timeout: 3000});

       //This will navigate to log in route because registration success
       this.router.navigate(['/login']);

     } else {

       this.flashMessage.show('Something went wrong', {cssClass: 'alert-
       danger', timeout: 3000});
       this.router.navigate(['/register']);
     }
  })

}

}

Use a Date Object :

var date = new Date();

MDN documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM