From d1638755778a8df890afd1fed0b6e5324c6e0795 Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Sun, 12 Dec 2010 13:23:00 -0800 Subject: [PATCH 1/3] added email validation. The border of the input field becomes red if the input seems to be invalid. --- public/javascripts/validation.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/public/javascripts/validation.js b/public/javascripts/validation.js index 24c8fdf2e..97c4b43e0 100644 --- a/public/javascripts/validation.js +++ b/public/javascripts/validation.js @@ -7,6 +7,9 @@ var Validation = { username: { characters: /^(|[A-Za-z0-9_]{0,32})$/, length: [6, 32] + }, + email: { + characters: /^(([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,}))(, *(([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})))*$/ } }, events: { @@ -15,10 +18,19 @@ var Validation = { if(!Validation.rules.username.characters.test(this.value + String.fromCharCode(evt.keyCode))) { evt.preventDefault(); } + }, + emailKeypress: function(evt) { + if(evt.charCode === 0) { return; } + if(!Validation.rules.email.characters.test(this.value + String.fromCharCode(evt.charCode))) { + $('#user_email').css('border-color', '#8B0000'); + } else { + $('#user_email').css('border-color', '#666666'); + } } } }; $(function() { $("#user_username").keypress(Validation.events.usernameKeypress); + $("#user_email").keypress(Validation.events.emailKeypress); }); From 4cc62ffacab0e43ff64985afc2aefc9ea124445b Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Mon, 13 Dec 2010 08:38:02 +0100 Subject: [PATCH 2/3] added spec for email validation --- spec/javascripts/validation-spec.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/javascripts/validation-spec.js b/spec/javascripts/validation-spec.js index ab1aa8931..f289f926d 100644 --- a/spec/javascripts/validation-spec.js +++ b/spec/javascripts/validation-spec.js @@ -7,6 +7,13 @@ describe("Validation", function() { }); }); }); + describe("email", function() { + describe("characters", function() { + it("is the regex for checking if the input is a valid list of e-mail addresses", function() { + expect((typeof Validation.rules.email.characters.test === "function")).toBeTruthy(); + }); + }); + }); }); describe("events", function() { describe("usernameKeypress", function() { @@ -16,5 +23,16 @@ describe("Validation", function() { expect(Validation.rules.username.characters.test("ffffffffffffffffffffffffffffffffff")).toBeFalsy(); }); }); + describe("emailKeypress", function() { + it("colors the border red if the input seems to be a invalid list", function() { + expect(Validation.rules.email.characters.test("user@example.com")).toBeTruthy(); + expect(Validation.rules.email.characters.test("user@example.com, user@example.com")).toBeTruthy(); + expect(Validation.rules.email.characters.test("user@example.com, user@example.com, user@example.com")).toBeTruthy(); + expect(Validation.rules.email.characters.test("user@example.com user@example.com")).toBeFalsy(); + expect(Validation.rules.email.characters.test("user@examplecom")).toBeFalsy(); + expect(Validation.rules.email.characters.test("userexample.com")).toBeFalsy(); + expect(Validation.rules.email.characters.test("userexamplecom")).toBeFalsy(); + }); + }); }); }); From 9a873aa760e1889aba9e8334b37ad9a29156e09a Mon Sep 17 00:00:00 2001 From: Mark Schmale Date: Mon, 13 Dec 2010 15:48:36 +0100 Subject: [PATCH 3/3] added fix for Bug #695 to email-validation --- public/javascripts/validation.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/javascripts/validation.js b/public/javascripts/validation.js index 97c4b43e0..923fb7fce 100644 --- a/public/javascripts/validation.js +++ b/public/javascripts/validation.js @@ -20,8 +20,8 @@ var Validation = { } }, emailKeypress: function(evt) { - if(evt.charCode === 0) { return; } - if(!Validation.rules.email.characters.test(this.value + String.fromCharCode(evt.charCode))) { + if(evt.keyCode === 0) { return; } + if(!Validation.rules.email.characters.test(this.value + String.fromCharCode(evt.keyCode))) { $('#user_email').css('border-color', '#8B0000'); } else { $('#user_email').css('border-color', '#666666');