Vue email validation is failing to return a valid email address

I'm relatively new to Vue and have implemented email validation using the reg expression in my Vue script data for this project. By utilizing

console.log(this.reg.test(this.email))
and observing the output while users input their email, the validation works correctly by returning true or false. However, I'd like the 'NEXT' button to become enabled only when
console.log(this.reg.test(this.email))
is true.

View

<div id="app">
  <h2>To-Dos:</h2>
  <input type="email" v-model="email" placeholder="Enter your email address"/>
  <button v-bind:disabled="isDisableComputed">NEXT</button>
</div>

Script

new Vue({
  el: "#app",
  data: {
    email: '',
    reg: /^(([^<>()\[\]\\.,;:\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,24}))$/
  },
  methods: {
    toggle: function(todo){
        todo.done = !todo.done
    }
  },
  
  computed: {
      isDisableComputed() {
      if(this.reg.test(this.email)){
        console.log(this.reg.test(this.email));
        return false;

      }
      else{
        console.log(this.reg.test(this.email));
        return true;
      }

    },
    
   }

})

You can view my code on JSFIDDLE here:

https://jsfiddle.net/ujjumaki/9es2dLfz/6/

Answer №1

Check out the MDN: RegExp for information on how RegExp.test returns a boolean, not a string. Therefore, the statement

this.reg.test(this.email) == 'true'
will always be false.

let regex = /^(([^<>()\[\]\\.,;:\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,24}))$/

console.log("regex.test('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7d3c2d4d3e7d3c2d4d389c4c8ca">[email protected]</a>')      ==> ", regex.test('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="93e7f6e0e7d3e7f6e0e7bdf0fcfe">[email protected]</a>'))
console.log("regex.test('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="394d5c4a4d794d5c4a4d175a5654">[email protected]</a>')=='true'  ==> ", regex.test('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfcbdacccbffcbdacccb91dcd0d2">[email protected]</a>') == 'true')
console.log("regex.test('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a0e1f090e3a0e1f090e54191517">[email protected]</a>')      ==> ", regex.test('test@<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7105140205315f121e1c">[email protected]</a>'))
console.log("regex.test('test@<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7a3b2a4a397f9b4b8ba">[email protected]</a>')=='true'  ==> ", regex.test('test@<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9eeafbedeadeb0fdf1f3">[email protected]</a>') == 'true')

Instead of using

return !this.reg.test(this.email)
, consider using it as a computed property like isDisableComputed in the example provided below.

new Vue({
  el: "#app",
  data () {
    return {
      email: '',
      reg: /^(([^<>()\[\]\\.,;:\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,24}))$/
    }
  },
  methods: {
    toggle: function(todo){
        todo.done = !todo.done
    }
  },
  computed: {
    isDisableComputed() {
        return !this.reg.test(this.email)
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <h2>Todos:</h2>
  <input type="email" v-model="email" placeholder="enter email email address"/>
  <button :disabled="isDisableComputed">NEXT ({{isDisableComputed}})</button>
</div>

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

"Could you please provide me with further details on how to use sequelize

I recently started working with node js and I used sequelize-auto to generate the models. However, I encountered an error when following the guidance provided in this link https://github.com/sequelize/sequelize-auto. If you need further clarification on th ...

Converting milliseconds to a valid date object using Angular form validation

I am facing an issue with form validation and milliseconds in my Angular application. It seems that Angular does not consider time in milliseconds as a valid date format, causing the angular.isDate(1418645071000) function to return false. How can I modify ...

A more efficient method for passing a function as an argument using the keyword "this"

When it comes to passing a function as an argument that uses the this keyword, is there a preferred method or correct way to do so? An example would be helpful in understanding this concept better: For instance, if I want to fade out an object and then r ...

When employing UI-Router, custom directives may not function properly within nested views

I was developing an angular application using phonegap, ionic, and angular. I had created a custom directive that registered an event listener for the element to activate iScroll upon loading. Initially, the directive worked perfectly when all the views we ...

Trouble with disabling default actions and transferring text

When the user clicks on loginAccount, the intention is to extract the text from the element with the id input-1 and assign it to username. This same process should occur for password, followed by form submission. However, despite using e.preventDefault() ...

Using JavaScript's indexOf method with multiple search values

I have a data set that includes various duplicate values ["test234", "test9495", "test234", "test93992", "test234"] I am looking to find the positions of every instance of test234 in the dataset Although ...

Implement tooltip functionality in ssr chart using echarts

A chart is generated using echarts on the server-side: getChart.ts const chart = echarts.init(null, null, { renderer: 'svg', ssr: true, width: 400, height: 300 }); chart.setOption({ xAxis: { data: timeData }, ...

How can I retrieve and manipulate the text within an option tag of a form using JavaScript?

<select name="products"> <option value=""> - Choose - </option> <option value="01">table</option> <option value="02">chair</option> <option value="03">book</option> <option value= ...

Error: The property 'initializeApp' cannot be read because it is undefined

Recently, I decided to integrate Firebase libraries into my Vue project by installing them using npm install firebase. I then proceeded to add these Firebase libraries in my main.js file: import { Firebase } from 'firebase/app' import 'fir ...

Employing jQuery to extract the text from the h4 class="ng-binding" element beyond the Angular scope

Is it possible to retrieve the current text content of <h4 class="ng-binding"></h4>? The text content is generated dynamically within the angular.js setup. I am interested in finding a way to extract this text using jQuery or JavaScript from ...

Break down React website into distinct modules and bundle them as npm dependencies within a single package

Currently, I am developing a React website that includes distinct sections such as contact management and message management. Each of these sections is quite extensive. To navigate to these sections, we use a single dashboard for control. Since separate ...

Ways to determine cleanliness or filthiness in an Angular 5 modal form?

I have a form within a modal and I only want to submit the form if there are any changes made to the form fields. Here is a snippet of my form: HTML <form [formGroup]="productForm" *ngIf="productForm" (ngSubmit)="submitUpdatedRecord(productForm.value) ...

Exploring portfinder in Javascript: A guide to its usage

As a newcomer to Javascript, I am eager to figure out how to utilize the portfinder.getPort() function within one of my functions in order to generate a random port each time. The code snippet below showcases my current implementation: var portfinder = re ...

Unable to establish connection through MQTT 1884 protocol

Currently, my website's messaging system is powered by MQTT. While everything is functioning smoothly on my local machine, I encounter an error when trying to use the system on the production site: vendor.bbaf8c4….bundle.js:1 WebSocket connection ...

Eliminating choices from a dropdown list using jQuery

I am currently working on a page that contains 5 dropdown menus, all of which have been assigned the class name 'ct'. During an onclick event, I want to remove the option with the value 'X' from each dropdown menu. My current code snipp ...

What is the best way to make my text stand out against a faded background?

After mastering the basics of web development on Codecademy, I was eager to start my own blog and create a unique website. One challenge I encountered was figuring out how to fade the background without affecting the text. Despite researching various solut ...

Ways to display an SVG spinner prior to a substantial UI refresh

I am currently facing an issue with updating 10 apexcharts bar charts simultaneously in a Vue app. When this process occurs, it takes approximately one second to load completely, and during that time, I would like to display an svg spinner. However, the co ...

Guide to incorporating Bootstrap and its dependencies into a Chrome extension powered by Vue using npm

As I delve into the world of webpack, vue, and vuex to create a chrome extension, I encountered an issue with loading Bootstrap 4 within the extension. Despite using the correct path for the node modules folder, I keep getting a file not found error when t ...

Experiencing memory issues while attempting to slice an extensive buffer in Node.js

Seeking a solution for efficiently processing a very large base64 encoded string by reading it into a byte (Uint8) array, splitting the array into chunks of a specified size, and then encoding those chunks separately. The current function in use works but ...

What is the best way to create a function that triggers another function every minute?

Currently, I have a function that checks if a user is authenticated: isAuthenticated = (): boolean => { xxx }; I am working with AngularJS and I want to create a new function called keepCheckingAuthentication() This new function should call the ...