A
A
adizh2022-02-03 15:08:14
Vue.js
adizh, 2022-02-03 15:08:14

Regex not working in Vue2?

I needed to do email validation and vscode highlights the regex as an error.
regex
in computed is regex:

computed:{
  regex(){
    return  new RegExp(/^(([^<>()\[\]\\.,;:\[email protected]"]+(\.[^<>()\[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[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}))$/)

  }
},

validation:
if (!this.regex.test(this.email)) {
        this.emailErr = true;
        let vm = this;
        setTimeout(() => {
          vm.emailErr = false;
        }, 1000);

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Alexander, 2022-02-03
@Aleksandr-JS-Developer

Regex not working in Vue2?
Yes, the creators of Vue cut regular expressions out of JS.
Try it:

computed:{
  regex(){
    return /^(([^<>()\[\]\\.,;:\[email protected]"]+(\.[^<>()\[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[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}))$/;
  }
},

RegExp accepts a string, and you pass a regular expression
UPD : RegExp accepts regular expressions as well. Check the correctness of the expression

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question