2
// import Mantissa.LiveForm
5
This just does validation for Mantissa user-info-requiring signup right now,
6
but the principles could hopefully be applied to other forms of LiveForm
11
XXX TODO: I really want to say "package Mantissa.Validate" or something.
14
Mantissa.Validate.SignupForm = Mantissa.LiveForm.FormWidget.subclass(
15
"Mantissa.Validate.SignupForm");
17
Mantissa.Validate.SignupForm.methods(
18
function __init__(self, node) {
19
Mantissa.Validate.SignupForm.upcall(self, '__init__', node);
20
self.domain = document.location.hostname;
21
var domarr = self.domain.split(".");
22
if (domarr[0] === 'www') {
24
self.domain = domarr.join('.');
27
var junk = self.gatherInputs();
28
for (var yuck in junk) {self.inputCount++;}
29
// minus one for domain, plus one for confirm...
30
self.verifiedCount = 0;
31
self.testedInputs = {};
33
self.passwordInput = self.nodeByAttribute("name", "password");
35
self.submitButton = self.nodeByAttribute("type", "submit");
38
// see LiveForm for explanation
39
function runFader(self, fader) {
40
return fader.fadeIn();
43
function submitSuccess(self, result) {
44
var d = Mantissa.Validate.SignupForm.upcall(self, 'submitSuccess', result);
45
d.addCallback(function() {
46
window.location = '/login';
51
function defaultUsername(self, inputnode) {
52
if (inputnode.value.length == 0) {
53
inputnode.value = (self.nodeByAttribute("name", "firstName").value.toLowerCase()
55
self.nodeByAttribute("name", "lastName").value.toLowerCase());
58
function verifyNotEmpty(self, inputnode) {
60
This is bound using an athena handler to all the input nodes.
62
We need to look for a matching feedback node for this input node.
64
self.verifyInput(inputnode, inputnode.value != '');
66
function verifyUsernameAvailable(self, inputnode) {
67
var username = inputnode.value;
68
self.callRemote("usernameAvailable",username, self.domain).addCallback(
70
self.verifyInput(inputnode, result);
73
function verifyStrongPassword(self, inputnode) {
76
inputnode.value.length > 4);
78
function verifyPasswordsMatch(self, inputnode) {
81
(self.testedInputs['password']) &&
82
(inputnode.value === self.passwordInput.value));
84
function verifyValidEmail(self, inputnode) {
86
var addrtup = inputnode.value.split("@");
87
// require localpart *and* domain
88
if (addrtup.length == 2) {
89
var addrloc = addrtup[0];
90
// localpart can't be empty
91
if (addrloc.length > 0) {
92
// domain can't be empty
93
var addrdom = addrtup[1].split('.');
94
if (addrdom.length >= 1) {
95
for (var i = 0; i < addrdom.length; i++) {
97
if (i === (addrdom.length - 1)) {
98
// TLDs are all 2 or more chars
101
// other domains can be one letter
104
if (addrdom[i].length < requiredLength) {
120
self.verifyInput(inputnode, cond);
122
function verifyInput(self, inputnode, condition) {
123
var statusNode = self._findStatusElement(inputnode);
125
var wasPreviouslyVerified = !!self.testedInputs[inputnode.name];
128
statusNode.style.backgroundColor = 'green';
130
statusNode.style.backgroundColor = 'red';
133
if (condition != wasPreviouslyVerified) {
134
self.testedInputs[inputnode.name] = condition;
136
self.verifiedCount++;
138
self.verifiedCount--;
140
self.submitButton.disabled = !(
141
self.verifiedCount === self.inputCount);
144
function _findStatusElement(self, inputnode) {
145
var fieldgroup = inputnode.parentNode;
146
while (fieldgroup.getAttribute("class") != "verified-field") {
147
fieldgroup = fieldgroup.parentNode;
149
var theNodes = fieldgroup.childNodes;
150
for (var maybeStatusNodeIdx in theNodes) {
151
var maybeStatusNode = theNodes[maybeStatusNodeIdx];
152
if (typeof maybeStatusNode.getAttribute != 'undefined') {
153
if (maybeStatusNode.getAttribute("class") == "verify-status") {
154
return maybeStatusNode;
159
function gatherInputs(self) {
160
inputs = Mantissa.Validate.SignupForm.upcall(
161
self, 'gatherInputs');
162
delete inputs['confirmPassword'];
163
delete inputs['__submit__'];
164
// screw you, hidden fields!
165
inputs['domain'] = [self.domain];