diff --git a/app/views/invitations/edit.html.haml b/app/views/invitations/edit.html.haml index 1eac967f6..7b315050b 100644 --- a/app/views/invitations/edit.html.haml +++ b/app/views/invitations/edit.html.haml @@ -1,3 +1,5 @@ += javascript_include_tag "validation" + = form_for(resource, :as => resource_name, :url => invitation_path(resource_name), :html => {:method => :put }) do |f| %p = f.label :username diff --git a/app/views/registrations/new.html.haml b/app/views/registrations/new.html.haml index a8f54e153..0cf3fc759 100644 --- a/app/views/registrations/new.html.haml +++ b/app/views/registrations/new.html.haml @@ -1,3 +1,5 @@ += javascript_include_tag "validation" + .span-12.prepend-6.last .floating %h3 diff --git a/public/javascripts/validation.js b/public/javascripts/validation.js new file mode 100644 index 000000000..0a7279444 --- /dev/null +++ b/public/javascripts/validation.js @@ -0,0 +1,23 @@ +/* Copyright (c) 2010, Diaspora Inc. This file is + * licensed under the Affero General Public License version 3 or later. See + * the COPYRIGHT file. + */ +var Validation = { + rules: { + username: { + characters: /^(|[A-Za-z0-9_]{0,32})$/, + length: [6, 32] + } + }, + events: { + usernameKeypress: function(evt) { + if(!Validation.rules.username.characters.test(this.value + String.fromCharCode(evt.charCode))) { + evt.preventDefault(); + } + } + } +}; + +$(function() { + $("#user_username").keypress(Validation.events.usernameKeypress); +}); diff --git a/spec/javascripts/support/jasmine.yml b/spec/javascripts/support/jasmine.yml index 61c9341fe..1b5cb5a34 100644 --- a/spec/javascripts/support/jasmine.yml +++ b/spec/javascripts/support/jasmine.yml @@ -14,6 +14,7 @@ src_files: - public/javascripts/vendor/jquery144.js - public/javascripts/vendor/jquery-ui-1.8.6.custom.min.js - public/javascripts/vendor/jquery.tipsy.js + - public/javascripts/validation.js - public/javascripts/diaspora.js - public/javascripts/mobile.js - public/javascripts/aspect-edit.js diff --git a/spec/javascripts/validation-spec.js b/spec/javascripts/validation-spec.js new file mode 100644 index 000000000..e896d0db5 --- /dev/null +++ b/spec/javascripts/validation-spec.js @@ -0,0 +1,20 @@ +describe("Validation", function() { + describe("rules", function() { + describe("username", function() { + describe("characters", function() { + it("is the regex for checking if we allow what the user typed", function() { + expect(typeof Validation.rules.username.characters).toEqual("function"); + }); + }); + }); + }); + describe("events", function() { + describe("usernameKeypress", function() { + it("doesn't allow the user to type anything but letters, numbers and underscores", function() { + expect(Validation.rules.username.characters.test("*")).toBeFalsy(); + expect(Validation.rules.username.characters.test("Aa_")).toBeTruthy(); + expect(Validation.rules.username.characters.test("ffffffffffffffffffffffffffffffffff")).toBeFalsy(); + }); + }); + }); +});