Tips for resetting the form input fields in Vue.js after the user has successfully submitted the form

I am facing an issue with my registration page. After the user enters some values and successfully submits the form, I want to clear all the fields. To achieve this, I am using a predefined function called reset() inside the script section. However, the function is not working as expected, and I'm unable to identify where I went wrong. Another problem I encountered is that autocomplete="off" is also not functioning properly. Can someone please help me resolve these issues? Register.vue

<template>
<div class="main">
    <div class="container">
        <img src="../assets/sideImg.png" alt="notFound" />
        <p>Online Book Shopping</p>
        <div class="box">
            <div class="headings">
                <h5 class="signin">Login</h5>
                <h5 class="signup">signup</h5>
            </div>
            <form id="myForm"  @submit.prevent="handlesubmit">
                <div class="fullname">
                    <p>FullName</p>
                    <input type="text" class="namebox"  required v-model="FullName" autocomplete="off" pattern="[A-Za-z]{3,12}">
                </div>
                <div class="username">
                    <p>EmailID</p>
                    <input type="email" class="emailbox" required v-model="EmailID" pattern="^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$">
                </div>
                <div class="pass">
                    <p>Password</p>
                    <input :type="password_type" class="password" v-model="Password" pattern="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{6,}$" required>
                    <i class="bi bi-eye-slash" id="togglePassword"></i>
                </div>
                <div class="mobile">
                    <p>MobileNumber</p>
                    <input type="tel" class="telephone" v-model="Mobile" pattern="^\d{10}$" required>
                </div>
                <button class="btn-section" @click="clear();" type="submit">Signup</button>
            </form>
        </div>
    </div>
</div>
</template>

<script>
import service from '../service/User'
export default {
    name: 'Register',
    data() {
        return {
            FullName: '',
            EmailID: '',
            Password: '',
            Mobile: '',
            password_type: "password",
        }
    },
    methods: {
        togglePassword() {
            this.password_type = this.password_type === 'password' ? 'text' : 'password'
        },
        clear(){
            document.getElementById("myForm").reset();
        },
         handlesubmit() {
            let userData = {
                FullName: this.FullName,
                EmailID: this.EmailID,
                Password: this.Password,
                Mobile: this.Mobile
            }
            service.userRegister(userData).then(response => {
                // console.log("user Details", response);
                alert("user registered successfully");
                return response;
            }).catch(error => {
                alert("Invalid Email/Mobile number");
                return error;
            })
        }
    }
}
</script>

<style lang="scss" scoped>
@import "@/styles/Register.scss";
</style>

Answer №1

Here is a suggestion for you:

If you are working on your HTML code, consider the following:

<form ref="myForm" @submit.prevent="handlesubmit">

Once the function handlesubmit is done executing, you can reset the form like this:

this.$refs.myForm.reset();

Regarding autocomplete, you can check out more information here:

To enable autocompletion, user-agents may need certain elements to:

  1. Have a name and/or id attribute

  2. Be descendants of a <form> element

  3. The form should have a submit button

Consider adding a name or id to the input field.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Remove the background color from a semi-transparent circular CSS div when it is being dragged

My circular div is being dragged for a drag and drop operation, but there's always a translucent square around it. How can I remove this unwanted effect? body{ background : #BBD1DF; } .dragdemo { width: 170px; height: 170px; line-hei ...

Vue 3 - Child Component Script Not Updating with Reactive Prop Changes

I am facing an issue where I am trying to pass a reactive data as a prop to a child component in Vue 3. The data updates correctly in the child component's template, but it does not reflect in the child component's script. In the parent component ...

jQuery Dialog interface endlessly scrolls to the top

While debugging code for a project, I came across the use of jquery UI Dialog for popups. However, there seems to be an issue where the page keeps scrolling to the top while the dialog remains stationary wherever it was opened. Below is the code snippet in ...

What is the best way to implement CSS Background-size: cover; while also making room for a menu?

Recently, I came across the CSS property background-size: cover, but I encountered a problem. I want the area behind my navigation on the left to be black. However, when I leave black space in the image itself, it gets cropped off when the image resizes. ...

Logging DOM elements with Electron's webview preload feature

Within my Electron program, I am utilizing a webview in my index.html file. The webview is equipped with a preloader, and my goal is to manipulate the DOM of the webview specifically, not just the index.html file. In my preloader code snippet below, the c ...

Error 400: Invalid Request: Issue encountered when swapping code for Asana access token using Next.js

Encountered a 400 Bad Request error while trying to exchange the code for an access token at . I am unsure of the cause and would appreciate any assistance. Below is the code: const GetAsanaAccessToken = async (req, res) => { const body = { grant ...

Can you guide me on how to configure the system proxy settings on Windows operating system?

I'm working on developing a VPN application for Windows and I am interested in setting up a proxy on the system. While there are various methods to accomplish this (such as through the registry or command line), I am searching for a more efficient so ...

Generate Bootstrap 5 Navbar <li> and <ul dropdown item> dynamically using JavaScript

Requesting assistance I have stored text in the constant.js file as shown below. export const navLinks = [ { id: "manage", title: "Manage", }, { id: "transactions", title: "Tran ...

The enigma of CSS: How is the width mysteriously set to 0px with no visible CSS styling

Take a look at this snapshot of a webpage I'm currently designing in Chrome Developer Tools: https://i.sstatic.net/SB00j.png The primary rule in the 'Matched CSS Rules' section indicates that the width of the element should be 160px. Howe ...

The jqueryMobile Dialog persistently opens during pageshow, despite having the cookie already set

My jQuery mobile dialog keeps opening every time the page is refreshed, despite having a cookie set to open it only once. I can't figure out why it's loading without any triggers. Any assistance would be greatly appreciated. JAVASCRIPT function ...

What is the best way to connect a data property to dynamic elements?

Once the page loads, an SVG file's content will be injected into the page. Is there a method to connect a data property to an element within the SVG in order to achieve something like this: <g :data-type="type">...</g> or perhaps this: ...

Adding a subtle gradient to the image, just ahead of the SVG

Is there a way to achieve this particular look? https://i.stack.imgur.com/yQjvK.png I'm currently encountering this appearance issue. https://i.stack.imgur.com/eQjQ4.png Any suggestions on how I can make it match my desired outcome? Your assistan ...

What is the best way to create a personalized image as the background in WordPress using CSS for a client?

I have this in my style.css .showcase{ background: url("x.jpg") no-repeat 0; } My website is built on WordPress and I have implemented advanced custom fields for the client to edit text. However, I am struggling to find a way for them to change the ...

Transforming a REST API get call into GraphQL

I'm currently using a REST API that accepts a code parameter, searches the database for matches, returns results if the parameter exists, and redirects users to a long_url retrieved from the database. How can I convert this functionality to GraphQL? i ...

The optimal approach for AngularJS services and promises

I have a unique setup in my AngularJS application where I utilize services to make API calls using $http and handle promises in my controllers. Here's an example of my current approach: app.service('Blog', function($http, $q) { var deferr ...

Guide to clicking on a user and displaying their details in a different component or view using Vue.js router

Currently working on a Vuejs project that requires the following tasks: Utilize an API to fetch and display a list of users (only user names) on the home page. Create a custom search filter to find users based on their names. Implement functionalit ...

The designated redirection path, as indicated in the 'next.config.js' file for a particular project, has now been applied to all projects

Something strange is happening... I set a redirect path for the root index page in one of my projects and it worked perfectly, but now all of my other projects are also being redirected to that same path when I try to visit localhost:3000. It's alway ...

Struggling to make jQuery code function properly in Wordpress, despite attempting to use noConflict

I have created a custom image grid gallery in WordPress using HTML and CSS, complete with popups and sliders. I had to code it myself because I couldn't find a suitable plugin that matched my design preferences. I have tested the functionality on my ...

Switching Divs Based on Radio Button Selection in VueJS

How can I display different components based on radio button selection? <input type="radio" name="book" value="One" checked="checked"> <input type="radio" name="book" value="Round"> <div> // initial display when "One" is selecte ...

Convert an array of objects into an object where the keys are determined by the value of a specific

I'm working with an array that looks like this: const inventory = [ { fruit: 'apple', quality: 'good', quantity: 10 }, { fruit: 'banana', quality: 'average', quantity: 5 }, { fruit: 'orange', qua ...