0
votes

Versions

vee-validate: 2.1.0-beta.2
vue: 2.5.7

Description

Used the email validation but it doesn't consider some valid emails such as:

It considers:


Here's the code snippet:
[<input type="email" 
    id="email" 
    name="email" 
    class="form-control" 
    placeholder="Email " 
    v-model="user.email" 
    v-validate="'required|email'" 
/> 
<span class="error-msg" v-show="errors.has('email')">{{ errors.first('email') }}</span>][1]

See image for reference: https://i.stack.imgur.com/Dpz4F.png

---------------[UPDATE]:---------------
I override the email rule and now here it is:

const email = {
  getMessage(field, args) {
    return `The ${field} must be a valid email`;
  },
  validate(value, args) {
    const EMAILREG = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
    
    console.log(EMAILREG.test(value));
    console.log(VeeValidate.Rules.email(value));
    return VeeValidate.Rules.email(value) || EMAILREG.test(value);;
  }
};

VeeValidate.Validator.extend('email', email);


What this custom rule is doing is even if it fails in the email validation of vee-validate, if it's true in the regEx validation, it will accept it. However, it consider admin@a as a valid email address.

1
But EMAILREG.test("admin@a") does test true, so is it the regex you want help with? - ippi
It looks like everything after @[a-zA-Z0-9] is optional. And if you want to follow the spec, admin@a IS a valid email address. - ippi
Hi @ippi, if there's a way to fix vee-validate email validation, I'll be using that instead. Since I saw alot of regEx here however, it also have pros and cons.. Or do you have any other way around? - Kyl San Antonio
@ippi the regEx is copied from jquery.validate.js (jsvalidation) - Kyl San Antonio
I suppose you want: /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/; (which would force a dot and at least one character after the dot. but the regex you have is good. "~@a" IS a valid email-address - it's only about if you want to follow the email spec or not. This is why we have click-a-link-in-your-email validation ;) - ippi

1 Answers

0
votes

May be you should replace * with + as * is for 0 or more occurences and + is for 1 or more occurences. So, replacing with + will ensure something to be there after .

^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$

Some Test Cases:

[email protected] - Pass
admin - Fail
[email protected] - Pass
[email protected] - Pass
[email protected] - Pass
[email protected] - Pass
admin@gmail - Fail
admin@ - Fail
@gmail.com - Fail

DEMO