react-redux-formal
Version:
Form state management and building library for react and redux
31 lines (29 loc) • 933 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = email;
/**
* Create a new email validator
*
* @param {String} [message] - The message to use if validator fails. Defaults to "Value is not a valid e-mail address"
*
* @return {Function} - The validator function
*/
function email() {
var message = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Value is not a valid e-mail address';
/**
* Check if value is a valid e-mail address
*
* @param {String} value - The value to validate
*
* @return {Promise} - Gets rejected if validator fails, resolved otherwise
*/
return function emailValidator(value) {
return new Promise(function (resolve, reject) {
if (typeof value !== 'string') return reject('Given value is not a string');
return (/^[^@.]+(\.[^@.]+)*@[^.]+(\.[^.]+)+$/.test(value) ? resolve(value) : reject(message)
);
});
};
}