﻿function validate(selector, regex, allowEmpty, invalidClassName) {

    var pattern = new RegExp(regex);
    var content = $(selector).val();
    var isValid = pattern.test(content) && (content != '');
    allowEmpty = allowEmpty && (content == '');

    if (allowEmpty || isValid) {
        $(selector).removeClass(invalidClassName);
        return true;
    }
    else {
        $(selector).addClass(invalidClassName);
        return false;
    }

}
