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

Commit

Permalink
simplify isEmail()
Browse files Browse the repository at this point in the history
close GH-9
  • Loading branch information
freewil committed Mar 3, 2014
1 parent 2ab6c5b commit db247ae
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ Field.prototype.customFilter = function (func) {

var MESSAGES = {
isDate: "%s is not a date",
isEmail: "%s is not an email address",
isUrl: "%s is not a URL",
isIP: "%s is not an IP address",
isAlpha: "%s contains non-letter characters",
Expand All @@ -225,7 +224,7 @@ var MESSAGES = {
};

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

Expand Down Expand Up @@ -308,6 +307,16 @@ Field.prototype.isFloat = Field.prototype.isDecimal = function (message) {
});
};

// super simple email validation
Field.prototype.isEmail = function (message) {
return this.add(function (value) {
if (typeof value != 'string' || !(/\S+@\S+/).test(value)) {
return { error: message || "%s is not an email address" };
}
return { valid: true };
});
};

Field.prototype.isString = function (message) {
return this.add(function (value) {
if (!object.isString(value)) {
Expand Down

0 comments on commit db247ae

Please sign in to comment.