Disabling the submit button in Vue.js by implementing error validation for input fields

 computed: {
    isDisabled: function(){

      return 
      !(this.errmsg.email < this.email.minemail) 
      
      }
      
 watch: {
      
    email(value){
    // binding this to the data value in the email input
    
    this.email = value;
  
  
// eslint-disable-next-line
    console.log("email",this.email);
    this.validateEmail(value);
    }
    },
    
    
    
   methods: {

 

      validateEmail(value) {
        // eslint-disable-next-line
      console.log("email",this.email,value);
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value))
        {
        this.errmsg['email'] = '';
        } else{
        this.errmsg['email'] = 'Invalid Email Address';
        }
        },
     }
<input type="text" v-validate="'required'" name="email" placeholder="Enter your company email ID" :maxlength="maxemail" id='email'  v-model='email'  />

 <div v-if="errmsg.email"  class="invalid-feedback-register">
 {{errmsg.email}} 
 </div>
 
  <button type="submit" :class="(isDisabled) ? '' : 'selected'"
 :disabled='isDisabled'  v-on:click=" isFirstScreen"  @click="persist"      >PROCEED</button>

Utilizing a watch function for email input validation which is functioning correctly. However, encountering an issue when attempting to disable the button based on {{errmsg.email}} as it is not working.

If the email is valid, enable the button, otherwise keep it disabled until the user enters the correct email id in the field.

Answer №1

I noticed a couple of errors in your code, particularly the comparison

this.errmsg.email < this.email.minemail
. I'm not sure why you're using the less than symbol < here.

Additionally, rather than monitoring every email id change, you can simply utilize the "input" event for the same functionality upon email changes.

Based on the variables provided above, I have adjusted the code below:

<template>
  <div> 
    <input type="text" v-validate="'required'" name="email" placeholder="Enter your company email ID" @input="validateEmail" :maxlength="maxemail" id='email' v-model="email"  />

    <div v-if="errmsg.email"  class="invalid-feedback-register">
    {{errmsg.email}} 
    </div>
    
      <button type="submit" :class="(isDisabled) ? '' : 'selected'"
    :disabled="isDisabled" @click="persist">PROCEED</button>   
  </div>
</template>

<script>
export default {
  data() {
    return {
      email: null,
      errmsg: { email: '' },
      maxemail: 60
    }
  },
  computed: {
    isDisabled () {
      return (!this.email || this.errmsg.email !== '')
    }
  },
  methods: {
    validateEmail() {
      // eslint-disable-next-line
      if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(this.email)) {
        this.errmsg.email = ''
      } else this.errmsg['email'] = 'Invalid Email Address';
      
    },
    persist () {

    }
  }
}

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

Creating a versatile tabbed interface with Vue.js for multiple uses

I am currently working on integrating a tabbed reusable component in vueJs, but I encountered an issue stating that a specific component is not defined. Below, you will find the code snippets for both components: //TabComponent <template> <di ...

Display PHP output for a brief moment

Here is the URL: http://www.example.com/?req=welcome. In PHP, to retrieve the parameter req, you can use the following code: echo $_GET['req']; The message will be displayed in the body but it should disappear after one second. What is the be ...

A quick guide on automatically checking checkboxes when the user's input in the "wall_amount" field goes over 3

I'm looking to have my program automatically check all checkboxes (specifically "Side 1, Side 2, Side 3 and Side 4") if the input for wall_amount is greater than 3. Is there a way to achieve this? I attempted lines 10-12 in JavaScript without success. ...

How to store the JSON response from a form submission in a JavaScript variable?

My HTML form has an action that sends a query to an external service. After submitting the form, I am redirected to the external website where JSON output is displayed. Is there a way for me to retrieve and store this object in a JavaScript variable? &l ...

Retrieve the HTML value of the most recently deleted element in CKEDITOR

Having used CKEditor for several months, I've come across an issue related to deletion within the editor. My Question: How can I retrieve the HTML value of the last element deleted in CKEditor? Upon clicking the Delete button, I aim to identify the ...

Generating charts using JSON data with the help of JavaScript

Hi there! I have a JSON file shown in the first image and I would like to create a chart similar to the one in the second image using JavaScript (using any library). Can anyone provide me with a simple example code on how to achieve this? ...

Manipulating JSON Elements using JavaScript in HTML

My objective is to alternate the visibility between the "prev" and "ext" elements retrieved from the JSON File in the following manner: Here is the link to JS Fiddle The menu bar appears as follows: [ "prev_Person1" "Person1" "ext_Person1" ... "Person2" ...

Facing issues with the functionality of a personalized AngularJS directive

I have created a custom directive using AngularJS 1.4.3. Below is the code for my directive: 'use strict'; angular.module('psFramework').directive('psFramework', function () { return { transclude: true, s ...

Steps to transfer the content of a label when the onclick event occurs

Seeking advice on how to send the dynamically varying value of a label upon clicking an anchor tag. Can anyone recommend the best approach to passing the label value to a JavaScript function when the anchor is clicked? Here is a sample code snippet: < ...

In React Native, what is the method for utilizing index.js rather than separate index.ios.js and index.android.js files to create a cross-platform app?

Thank you for the help so far, I am new to React Native, and I'm trying to develop a cross-platform app. Here is my index.js file: import React from 'react'; { Component, View, Text, } from 'react-nativ ...

Having an issue with collapsible feature when only one item is opened in AngularJS

I'm working on customizing a collapsible feature in my app. Instead of using the AngularJS UI Bootstrap accordion plugin, I opted to use transition.js and collapse.js from Bootstrap for a simpler design. My goal is to have 2 links next to each other w ...

Strategies for managing large numbers within WebGL GLSL shader programs

What is the best approach to handle large numbers like the one provided in GLSL? I have a shader that requires Date.now() as a uniform, which is defined as: The Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 ...

What is the best way to handle a promise passed as parameters to a Subject in RxJS?

When passing a Promise to Subject as a parameter: const work = new Subject<{ id: number; dialogRef: Promise<typeof Dialog> }>(); I aim to utilize the instance inside the promise at a later stage: ... exhaustMap(({ id, dialogRef }) => http ...

Developing a quiz using jQuery to load and save quiz options

code: http://jsfiddle.net/HB8h9/7/ <div id="tab-2" class="tab-content"> <label for="tfq" title="Enter a true or false question"> Add a Multiple Choice Question </label> <br /> <textarea name ...

Leveraging Javascript to retrieve PHP variables

I'm trying to figure out how to get access to PHP variables from Javascript when the PHP code is in a separate file. Here's the code I currently have: <?php $res = "OK"; ?> <html> <head> <script type="text/javascript"&g ...

"Toggling the parent element through jQuery within a search function

I have a similar structure in my table: <input id="mySearch" placeholder="Search..." type="text"/> <table> <th>Main header</th> <tbody id="searchable"> <tr class="parent"><td>Parent</td></tr> ...

Is it possible to utilize props within a computed property in order to apply a CSS class binding?

Can props be utilized within a computed property in an alternative manner? Upon attempting this, it seems to add the class 'foo' (the props name) instead of the intended 'fa-foo' (or a different value if props were configured). If I d ...

What is the significance of having multiple route parameters in Express?

The RESTful API provided by cex.io offers a route that can return pairs of all currencies with a variable amount of parameters. In express, how can we achieve similar functionality? Consider the following pseudo code example... app.get('/pairs/:arg ...

Enhance Your Browsing Experience with Ajax Chrome Extension

I tried sending the URL to a PHP file in a Chrome extension, but I'm having trouble getting a response. manifest.json { "name": "Get pages source", "version": "1.0", "manifest_version": 2, "description": "Get pages source from a popup", "b ...

Removing a portion of an item with the power of RxJS

I possess the subsequent entity: const myObject = { items:[ { name: 'John', age: 35, children: [ { child: 'Eric', age: 10, sex: 'M' }, { ...