aurelia / validation

A validation plugin for Aurelia.
MIT License
132 stars 129 forks source link

isEmail() validation incomplete #209

Closed go4cas closed 8 years ago

go4cas commented 8 years ago

First off, awesome plug-in!

I've noticed that when setting isEmail() on an input box, the following example passes validation: john@example

I would expect it to only pass if there's both a @ symbol and an email domain, e.g.: john@example.com

JoshMcCullough commented 8 years ago

Also noticed this. While it is technically a valid email address, I still think it is almost never what anyone would actually want!

heruan commented 8 years ago

Since as @JoshMcCullough said john@example is valid as e-mail, but commonly undesired, I suggest to add an optional boolean argument to have a strict/loose e-mail validation.

JoshMcCullough commented 8 years ago

I ended up simply overriding the default isEmail validator:

import {ValidationGroup} from 'aurelia-validation';
import moment from 'moment';

export class CustomValidation {
    constructor() {
        let validateEmail = (value) => {
            value = value.trim();

            if (value && !value.match(/^([\S\'-]+(?:\.[\S\'-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i))
                return false;

            return true;
        };

        this.validators = {
            isEmail: this.wrap((newValue, threshold) => {
                if (!validateEmail(newValue))
                    return 'is not a valid email';

                return threshold;
            })
        };
    }

    wrap(validator) {
        return function () {
            this.passes(validator);

            return this;
        };
    }

    configure() {
        var proto = ValidationGroup.prototype;

        for (var key in this.validators) {
            let validator = this.validators[key];

            proto[key] = validator;
        }
    }
}

Then simply import CustomValidation and call configure on the instance.

plwalters commented 8 years ago

Thanks for submitting this / commenting on this. At this time we are closing this because we have completely re-written and are deprecating the previous feature set. If you feel this should be re-opened please feel free to review this blog post and submit either again on this repository, or on the new validatejs bridge repository

Thanks again!

cornillemichiel commented 6 years ago

@PWKad Someone in our team stumbled on this using a recent version of aurelia-validation. Was this option then added? If yes it seems undocumented.