0

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:

  • admin@gmail.com

It considers:

  • admin@gmail.co
  • admin1@gmail.com
  • admin@gmail1.com
  • admin@gmail.net
  • admin@gmail.ne

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.

Aditya Kresna Permana
  • 11,869
  • 8
  • 42
  • 48
  • But EMAILREG.test("admin@a") does test true, so is it the regex you want help with? – ippi Jun 19 '18 at 02:56
  • 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 Jun 19 '18 at 02:59
  • 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 Jun 19 '18 at 03:01
  • @ippi the regEx is copied from jquery.validate.js (jsvalidation) – Kyl San Antonio Jun 19 '18 at 03:02
  • 1
    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 Jun 19 '18 at 03:04
  • The [email spec](http://www.ietf.org/rfc/rfc5322.txt) is pretty wild. I'm guessing even that jquery- regex is playing it strict (compared to the spec). – ippi Jun 19 '18 at 03:13
  • @ippi , thanks for the feedback. Will ask the client regarding there spec on email validation. Anyway, thanks for the regEx. :) – Kyl San Antonio Jun 19 '18 at 03:19

1 Answers1

0

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:

admin@gmail.com - Pass
admin - Fail
admin1@gmail.com - Pass
admin@gmail1.com - Pass
admin@gmail.net - Pass
admin@gmail.ne - Pass
admin@gmail - Fail
admin@ - Fail
@gmail.com - Fail

DEMO

Aman Chhabra
  • 3,824
  • 1
  • 23
  • 39
  • Just to put it out there, there are instances where a domain was misslept, an innocent typo such as `admin@gmail.coma`. Any thoughts on a case like this? – Qiniso Nov 04 '22 at 13:32