Implementing a button disable feature in Vuejs until email validation is complete

new Vue({
  el: '#app',
  data: {
    email: ''
  },
  computed: {
    isEmailValid() {
      return '/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/'.test(this.email)
    },
    isButtonDisabled: function() {
      return !this.email || this.isEmailValid;
    }
  }
})
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="app">
  <p>
    <input class="input-mobile-email" type="text" placeholder="Your email" id="email" v-model="email" name="email" />
  </p>
  <button :disabled='isButtonDisabled'>Send Form</button>
</div>

I have implemented a functionality where the button gets disabled until the email address is validated. I have created a computed property that checks if the entered email is valid and then disables the button accordingly. However, there seems to be an issue with the validation as the button remains disabled even after entering a correct email address.

To see the implementation in action, you can check out this jsfiddle link https://jsfiddle.net/k69cr0sf/2/

Answer №1

Your code has a couple of issues that need to be addressed:

  1. Make sure your regex is not surrounded by quotes like ''

  2. The isDisabled() function is returning an incorrect value because it evaluates true when this.isEmailValid == true, which is clearly wrong.

To simplify your logic, you can adjust your isDisabled() function to return the negated result of the regex test. For example: return !/.../.test(this.email)


new Vue({
  el: '#app',
  data: {
    email: ''
  },
  computed: {
    isDisabled: function() {
      return !/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(this.email)
    }
  }
})
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="app">
  <p>
    <input class="input-mobile-email" type="text" placeholder="Your email" id="email"
v-model="email" name="email" />
  </p>
  <button :disabled='isDisabled'>Send Form</button>
</div>

PS: To make your code snippets more comprehensive, consider adding external scripts by clicking on Add External scripts and providing the URL. This way, you can avoid linking to external jsfiddles for simpler cases and keep your questions self-sufficient.

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

There seems to be an issue with the React Native FlatList: It appears that there is no overload matching this call and some

I am currently learning React Native and attempting to create a basic chat room application. I am facing an issue with the FlatList component that I can't seem to resolve. Even though I have provided both the data prop and renderItem prop to the FlatL ...

The ReactJS component is unable to resolve the specified domain name

Whenever I utilize a component like const React = require('react'); const dns = require('dns'); class DnsResolver extends React.Component { componentDidMount() { dns.resolve('https://www.google.com', (err, addres ...

Empty array is logged by the server after sending a JavaScript variable through post request

When I use console.log(request.body), the terminal displays {} instead of logging the variable ownerSteamId. Here is my code: Server-side JavaScript: const express = require('express'); const app = express(); const bodyParser = require('bod ...

Using FB Messenger iOS to verify a third-party app

I am in the process of creating a web application that features Facebook authentication and the capability to invite friends through Facebook. While working on this project, I encountered a specific issue that is quite frustrating and I am hoping someone o ...

Learn how to implement a captivating animation with JavaScript by utilizing the powerful Raphael Library. Unleash the animation by triggering it with either

My desire is to indicate this movement by triggering a mouse click or drag to the desired location. let myDrawing = Raphael(10,10,400,400); let myCircle = myDrawing.circle(200,200,15); myCircle.attr({fill:'blue', stroke:'red'}); let my ...

Waiting for the code to execute once the filtering process is completed in Next.js using Javascript

I'm seeking a way to ensure that my code waits for the completion of my filter function before proceeding. The issue arises because my filter function, which incorporates another function called useLocalCompare, causes a delay in execution. This delay ...

Inquiring about how to make a Javascript HTTP POST request?

Imagine having a website with a snippet of javascript that, when triggered by a visitor clicking a button, sends an HTTP request to an external URL. Now, if the external URL which receives this request attempts to trace the IP address or host of the sourc ...

Adding a picture to the webpage and saving it temporarily on the site

After exploring all options on the site, testing it rigorously and closely following the instructions provided, I am still unable to determine where exactly I went wrong. Website Link: The main goal is to upload an image and temporarily store it within t ...

What are the best practices for managing live notifications with WebSocket technology?

I have developed a real-time chat application in React.js with Socket.io, but I want to implement a new feature. Currently, User A and User B can only communicate if they both have the chat open. I would like to notify User B with a popup/notification wh ...

How to disable typescript eslint notifications in the terminal for .js and .jsx files within a create-react-app project using VS Code

I'm currently in the process of transitioning from JavaScript to TypeScript within my create-react-app project. I am facing an issue where new ESLint TypeScript warnings are being flagged for my old .js and .jsx files, which is something I want to avo ...

"Utilizing Bootstrap Tour for Easy Navigation: Automatically Scroll Back to the Top with

I have a Bootstrap tour set up with code that is meant to capture the user pressing the end tour button and scroll them back to the top of the screen. However, currently the code runs when a new popover loads instead of when the end tour button is clicke ...

Mongo: executing queries with both .find() and .update() methods sequentially

I am looking to: Retrieve specific items from collection X; Retrieve other items from X; Make the items from step #1 have the same value as the items from step #2 Make the items from step #2 have the same value as the items from step #1 Essentially, ...

`Inconsistencies between Postman and AngularJS service responses`

When I make a call to the endpoint using Postman, I receive this response: https://i.stack.imgur.com/pH31G.png However, when I make the same request from my AngularJS service defined below: this.login = function (loginInfo) { return $http({ ...

Adjust the cursor style using Javascript

I am currently working on a paint software program and I have a feature where pressing the ctrl key while moving the mouse turns on the eraser. However, when the key is pressed, I want the cursor (currently a crosshair) to change to none. I have tried impl ...

Is it feasible to develop a functional computer interface using threejs?

Is it feasible to integrate a window into threejs that could facilitate the use of standard desktop applications (such as code editors) within the virtual scene? Please note: This is being implemented within a custom application or a node-webkit environme ...

Certain URLs do not receive requests when using jQuery.ajax(), while others are successful

Our Rails application features inline editing, allowing users to submit changes back to the server via PUT requests. The URL for the PUT request varies based on the object being edited and the page the user is on. The same JavaScript code supports this fea ...

Stub Node - Constructor Implementation

I've been searching for a solution for quite some time with no success, I have the following code that I want to test: some_script.js var Model = require('./models') exports.tokenizeCard = function (args) { var model = new Model(&apos ...

Unable to implement the checkCollision function within the Snake game using JavaScript

My latest project involves creating a Snake game using canvas in JavaScript. I've managed to set up most of the game, but I'm having trouble with the collision check. Whenever the snake hits itself or the wall, the game crashes because I can&apos ...

Aligning images with absolute CSS to text that is wrapping positions the images perfectly alongside the text content

My webpage has a long content section that resizes based on the browser width. Here's an example: <h1 id="loc1">Title</h1> <p>bodycopy bodycopy bodycopy bodycopy bodycopy bodycopy bodycopy bodycopy bodycopy bodycopy bodycopy bod ...

Dynamic Path Integration with Vuefire

How can I configure the path for Vuefire as shown below? export default { firebase: { classlist: db.ref('chapter/1'), // I want to get the number from data // E.g: db.ref('chapter/' + this.chapterid), }, data:{ chap ...