Skip to content
This repository was archived by the owner on Nov 27, 2018. It is now read-only.

Commit f246069

Browse files
committed
simplify isEmail()
close GH-9
1 parent ffd776d commit f246069

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

lib/field.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ Field.prototype.customFilter = function (func) {
212212

213213
var MESSAGES = {
214214
isDate: "%s is not a date",
215-
isEmail: "%s is not an email address",
216215
isUrl: "%s is not a URL",
217216
isIP: "%s is not an IP address",
218217
isAlpha: "%s contains non-letter characters",
@@ -225,7 +224,7 @@ var MESSAGES = {
225224
};
226225

227226
Object.keys(ValidatorPrototype).forEach(function (name) {
228-
if (name.match(/^(contains|notContains|equals|check|validate|assert|error|len|isNumeric|isDecimal|isFloat|regex|notRegex|is|not|notNull|isNull)$/)) {
227+
if (name.match(/^(contains|notContains|equals|check|validate|assert|error|len|isNumeric|isDecimal|isEmail|isFloat|regex|notRegex|is|not|notNull|isNull)$/)) {
229228
return;
230229
}
231230

@@ -308,6 +307,16 @@ Field.prototype.isFloat = Field.prototype.isDecimal = function (message) {
308307
});
309308
};
310309

310+
// super simple email validation
311+
Field.prototype.isEmail = function (message) {
312+
return this.add(function (value) {
313+
if (typeof value != 'string' || !(/\S+@\S+/).test(value)) {
314+
return { error: message || "%s is not an email address" };
315+
}
316+
return { valid: true };
317+
});
318+
};
319+
311320
Field.prototype.isString = function (message) {
312321
return this.add(function (value) {
313322
if (!object.isString(value)) {

0 commit comments

Comments
 (0)