aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/public/js/form-validators/accountValidator.js
diff options
context:
space:
mode:
authorDennis Eriksen <dennis.se@gmail.com>2013-03-20 12:43:33 +0100
committerDennis Eriksen <dennis.se@gmail.com>2013-03-20 12:43:33 +0100
commit266767e6b31d4799b218a7dc9ffd0d6f2eca92a8 (patch)
treee3e68b3acdfb276cadcc23d0028a6c1dd3bc8f21 /public/js/form-validators/accountValidator.js
parentEdited "Sign in" button - for fun (diff)
downloadDivid-266767e6b31d4799b218a7dc9ffd0d6f2eca92a8.tar.gz
login screen
Diffstat (limited to 'public/js/form-validators/accountValidator.js')
-rw-r--r--public/js/form-validators/accountValidator.js80
1 files changed, 80 insertions, 0 deletions
diff --git a/public/js/form-validators/accountValidator.js b/public/js/form-validators/accountValidator.js
new file mode 100644
index 0000000..6de2dfc
--- /dev/null
+++ b/public/js/form-validators/accountValidator.js
@@ -0,0 +1,80 @@
+
+function AccountValidator(){
+
+// build array maps of the form inputs & control groups //
+
+ this.formFields = [$('#name-tf'), $('#email-tf'), $('#user-tf'), $('#pass-tf')];
+ this.controlGroups = [$('#name-cg'), $('#email-cg'), $('#user-cg'), $('#pass-cg')];
+
+// bind the form-error modal window to this controller to display any errors //
+
+ this.alert = $('.modal-form-errors');
+ this.alert.modal({ show : false, keyboard : true, backdrop : true});
+
+ this.validateName = function(s)
+ {
+ return s.length >= 3;
+ }
+
+ this.validatePassword = function(s)
+ {
+ // if user is logged in and hasn't changed their password, return ok
+ if ($('#userId').val() && s===''){
+ return true;
+ } else{
+ return s.length >= 6;
+ }
+ }
+
+ this.validateEmail = function(e)
+ {
+ var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
+ return re.test(e);
+ }
+
+ this.showErrors = function(a)
+ {
+ $('.modal-form-errors .modal-body p').text('Please correct the following problems :');
+ var ul = $('.modal-form-errors .modal-body ul');
+ ul.empty();
+ for (var i=0; i < a.length; i++) ul.append('<li>'+a[i]+'</li>');
+ this.alert.modal('show');
+ }
+
+}
+
+AccountValidator.prototype.showInvalidEmail = function()
+{
+ this.controlGroups[1].addClass('error');
+ this.showErrors(['That email address is already in use.']);
+}
+
+AccountValidator.prototype.showInvalidUserName = function()
+{
+ this.controlGroups[2].addClass('error');
+ this.showErrors(['That username is already in use.']);
+}
+
+AccountValidator.prototype.validateForm = function()
+{
+ var e = [];
+ for (var i=0; i < this.controlGroups.length; i++) this.controlGroups[i].removeClass('error');
+ if (this.validateName(this.formFields[0].val()) == false) {
+ this.controlGroups[0].addClass('error'); e.push('Please Enter Your Name');
+ }
+ if (this.validateEmail(this.formFields[1].val()) == false) {
+ this.controlGroups[1].addClass('error'); e.push('Please Enter A Valid Email');
+ }
+ if (this.validateName(this.formFields[2].val()) == false) {
+ this.controlGroups[2].addClass('error');
+ e.push('Please Choose A Username');
+ }
+ if (this.validatePassword(this.formFields[3].val()) == false) {
+ this.controlGroups[3].addClass('error');
+ e.push('Password Should Be At Least 6 Characters');
+ }
+ if (e.length) this.showErrors(e);
+ return e.length === 0;
+}
+
+ \ No newline at end of file