Question

I'm new to the durandal application. Currently i was trying to validate the multiple email readed from the text area.

<textarea class="fullWidth" id="AddEmailSeparatedByComma"  rows="3" 
data-bind="value:emailList"></textarea>

I have readed multiple email seperated by "," and i was storing in

emailList = ko.observable("").extend({
 email: { message: "Please enter valid email address", params: true } 
});

I have provided validation message as above. Currently it is validating one email only, i don't know how to use customized regex here to validate multiple email which is separated by comma. Any help will be much appreciated.

Was it helpful?

Solution

Add another validation rule:

ko.validation.rules['emaillist'] = {
    validator: function (val, validate) {
        if (!validate) return true;
        if (ko.validation.utils.isEmptyVal(val)) return true;

        var emailrule = ko.validation.rules['email'];

        var vals = val.split(/\s*,\s*/);
        for (var i = 0; i < vals.length; i++) {
            if (!emailrule.validator(vals[i], true)) {
                return false;
            }
        }

        return true;
    },
    message: 'Please enter proper comma-separated email addresses'
};
ko.validation.addExtender('emaillist');

Fiddle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top