Veshraj Joshi
Veshraj Joshi

Reputation: 3579

regex rule for email validation is not working in vuejs

I tried this code as provided in https://v2.vuejs.org/v2/cookbook/form-validation.html.

validEmail: function (email) {
    var re = /^(([^<>()\[\]\\.,;:\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,}))$/;
    return re.test(email);
}

Vuejs is saying that - error: Unnecessary escape character: \[ (no-useless-escape) at src/components/form.vue:125:65 which is the line var re = ...

What I am getting wrong here? I called this function like this.validEmail(this.modelname)

Upvotes: 2

Views: 3765

Answers (1)

Decade Moon
Decade Moon

Reputation: 34306

The "error" (technically it's just a warning) is saying that you do not need to escape [ in the regex when it is inside a character set ([] syntax) because its meaning is unambiguous (you can't create nested character sets); ], on the other hand, does need to be escaped because it will be interpreted as part of the regex syntax that ends the character set instead of a literal ] character.

Simplified example:

/[\[]/
  ^ unnecessary escape

should be instead:

/[[]/

Upvotes: 3

Related Questions