@ -8,32 +8,60 @@
email _notify = document . getElementById ( 'email-notify' ) ,
password _notify = document . getElementById ( 'password-notify' ) ,
password _confirm _notify = document . getElementById ( 'password-confirm-notify' ) ,
usernamevalid = false ;
emailexists = false ,
emailvalid = false ,
userexists = false ,
passwordsmatch = false ;
passwordsmatch = false ,
passwordvalid = false ;
$ ( username ) . on ( 'keyup change' , function ( ) {
if ( username . value . length > 2 ) socket . emit ( 'user.exists' , { username : username . value } ) ;
else {
usernamevalid = utils . isUserNameValid ( username . value ) ;
if ( username . value . length < 3 ) {
username _notify . innerHTML = 'Username too short' ;
username _notify . className = 'label label-important' ;
}
else if ( ! usernamevalid ) {
username _notify . innerHTML = 'Invalid username' ;
username _notify . className = 'label label-important' ;
}
else {
socket . emit ( 'user.exists' , { username : username . value } ) ;
}
} ) ;
$ ( emailEl ) . on ( 'keyup change' , function ( ) {
socket . emit ( 'user.email.exists' , { email : emailEl . value } ) ;
emailvalid = utils . isEmailValid ( email . value ) ;
if ( ! emailvalid ) {
email _notify . innerHTML = 'Invalid email address' ;
email _notify . className = 'label label-important' ;
}
else
socket . emit ( 'user.email.exists' , { email : emailEl . value } ) ;
} ) ;
password . addEventListener ( 'keyup' , function ( ) {
if ( password . value . length < 5 ) {
$ ( password ) . on ( 'keyup' , function ( ) {
passwordvalid = utils . isPasswordValid ( password . value ) ;
if ( password . value . length < 6 ) {
password _notify . innerHTML = 'Password too short' ;
password _notify . className = 'label label-important' ;
} else {
} else if ( ! passwordvalid ) {
password _notify . innerHTML = 'Invalid password' ;
password _notify . className = 'label label-important' ;
} else {
password _notify . innerHTML = 'OK!' ;
password _notify . className = 'label label-success' ;
}
} , false ) ;
if ( password . value !== password _confirm . value ) {
password _confirm _notify . innerHTML = 'Passwords must match!' ;
password _confirm _notify . className = 'label label-important' ;
passwordsmatch = false ;
}
} ) ;
$ ( password _confirm ) . on ( 'keyup' , function ( ) {
if ( password . value !== password _confirm . value ) {
@ -63,14 +91,10 @@
socket . on ( 'user.email.exists' , function ( data ) {
emailexists = data . exists ;
emailvalid = isEmailValid ( email . value ) ;
if ( data . exists === true ) {
email _notify . innerHTML = 'Email Address exists' ;
email _notify . className = 'label label-important' ;
} else if ( ! emailvalid ) {
email _notify . innerHTML = 'Invalid email address' ;
email _notify . className = 'label label-important' ;
}
else {
email _notify . innerHTML = 'OK!' ;
@ -78,12 +102,6 @@
}
} ) ;
// from http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
function isEmailValid ( email ) {
var re = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/ ;
return re . test ( email ) ;
}
// Alternate Logins
var altLoginEl = document . querySelector ( '.alt-logins' ) ;
altLoginEl . addEventListener ( 'click' , function ( e ) {
@ -97,10 +115,10 @@
}
} ) ;
// Form Validation
function validateForm ( ) {
var validated = true ;
if ( username . value . length < 2 ) {
if ( username . value . length < 2 || ! usernamevalid ) {
username _notify . innerHTML = 'Invalid username' ;
username _notify . className = 'label label-important' ;
validated = false ;
@ -110,6 +128,10 @@
password _notify . innerHTML = 'Password too short' ;
validated = false ;
}
if ( password . value !== password _confirm . value ) {
password _confirm _notify . innerHTML = 'Passwords must match!' ;
}
if ( ! emailvalid ) {
email _notify . innerHTML = 'Invalid email address' ;
@ -121,10 +143,7 @@
validated = false ;
}
if ( userexists )
validated = false ;
if ( ! passwordsmatch )
if ( userexists || ! passwordsmatch || ! passwordvalid )
validated = false ;
return validated ;