Вопрос

My name, e-mail and check boxes follow the same js pattern and the validation works great but birthday will not validate. I start by stripping the white space so they can't leave it blank, then using a regex to determine if its valid. Is my regex wrong?

validate: function (attrs, options) 
{
    var errors = [],
    pattern = '/^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/',
    isValidBirthday = attrs.birthday && attrs.birthday.replace(/\s/g, '') && attrs.birthday === ('pattern');

    if (!isValidBirthday) 
    {
        errors.push(
        {
            "birthday": "Must have a valid birth date."
        });
    }

    if (errors.length) 
    {
        return errors;
    }
},
Это было полезно?

Решение

You are using the regular expression wrong. Try the following instead, noting the lack of '' around the regex, and the use of .test method on the pattern.

var errors = [],
    pattern = /^(0[1-9]|1[0-2])[-/.](0[1-9]|[12][0-9]|3[01])[-/.](19|20)\d\d$/,
    isValidBirthday,
    cleanedBirthday;

cleanedBirthday = attrs.birthday && attrs.birthday.replace(/\s/g, '');
isValidBirthday = pattern.test(cleanedBirthday)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top