@ -1,13 +1,17 @@
( function ( ) {
var username = document . getElementById ( 'username' ) ,
password = document . getElementById ( 'password' ) ,
password _confirm = document . getElementById ( 'password-confirm' ) ,
register = document . getElementById ( 'register' ) ,
emailEl = document . getElementById ( 'email' ) ,
username _notify = document . getElementById ( 'username-notify' ) ,
email _notify = document . getElementById ( 'email-notify' ) ,
password _notify = document . getElementById ( 'password-notify' ) ,
password _confirm _notify = document . getElementById ( 'password-confirm-notify' ) ,
emailexists = false ,
userexists = false ;
emailvalid = false ,
userexists = false ,
passwordsmatch = false ;
$ ( username ) . on ( 'keyup change' , function ( ) {
if ( username . value . length > 2 ) socket . emit ( 'user.exists' , { username : username . value } ) ;
@ -17,38 +21,69 @@
}
} ) ;
emailEl. addEventListener ( ' change', function ( ) {
$( emailEl ) . on ( 'keyup change', function ( ) {
socket . emit ( 'user.email.exists' , { email : emailEl . value } ) ;
} , false ) ;
} ) ;
password . addEventListener ( 'keyup' , function ( ) {
if ( password . value . length < 5 ) {
password _notify . innerHTML = 'Password too short' ;
password _notify . className = 'label label-important' ;
} else {
password _notify . innerHTML = '' ;
password _notify . innerHTML = 'OK!' ;
password _notify . className = 'label label-success' ;
}
} , false ) ;
$ ( password _confirm ) . on ( 'keyup' , function ( ) {
if ( password . value !== password _confirm . value ) {
password _confirm _notify . innerHTML = 'Passwords must match!' ;
password _confirm _notify . className = 'label label-important' ;
passwordsmatch = false ;
}
else {
password _confirm _notify . innerHTML = 'OK!' ;
password _confirm _notify . className = 'label label-success' ;
passwordsmatch = true ;
}
} ) ;
ajaxify . register _events ( [ 'user.exists' , 'user.email.exists' ] ) ;
socket . on ( 'user.exists' , function ( data ) {
userexists = data . exists ;
if ( data . exists == true ) {
if ( data . exists == = true ) {
username _notify . innerHTML = 'Username exists' ;
username _notify . className = 'label label-important' ;
} else {
username _notify . innerHTML = 'Not taken' ;
username _notify . innerHTML = ' OK! ';
username _notify . className = 'label label-success' ;
}
} ) ;
socket . on ( 'user.email.exists' , function ( data ) {
emailexists = data . exists ;
emailvalid = isEmailValid ( email . value ) ;
console . log ( 'derp' ) ;
if ( data . exists === true ) {
email _notify . innerHTML = 'Email Address exists' ;
} else {
email _notify . innerHTML = '' ;
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!' ;
email _notify . className = 'label label-success' ;
}
} ) ;
// 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 ) {
@ -74,17 +109,13 @@
if ( password . value . length < 5 ) {
password _notify . innerHTML = 'Password too short' ;
validated = false ;
} else {
password _notify . innerHTML = '' ;
}
}
if ( email . value. in dexOf( '@' ) === - 1 ) {
if ( ! emailvalid ) {
email _notify . innerHTML = 'Invalid email address' ;
validated = false ;
} else {
email _notify . innerHTML = '' ;
}
}
if ( emailexists ) {
email _notify . innerHTML = 'Email Address exists' ;
validated = false ;
@ -92,10 +123,15 @@
if ( userexists )
validated = false ;
if ( ! passwordsmatch )
validated = false ;
return validated ;
}
register . addEventListener ( 'click' , function ( e ) {
if ( ! validateForm ( ) ) e . preventDefault ( ) ;
} , false ) ;
} ( ) ) ;
} ( ) ) ;