$(function() { var name = $("#Rusername"), email = $("#Remail"), password = $("#Rpassword"), cpassword = $("#Rcpassword"), country = $("#Rcountry"), allFields = $([]).add(name).add(email).add(password).add(cpassword), tips = $("#validateTips"), result = $("#resultTips"); function fixSubmit() { var submitButton = $(".ui-dialog > .ui-dialog-buttonpane > button:first"); submitButton.removeAttr('disabled'); submitButton.text('Register an account'); submitButton.removeClass("ui-state-focus ui-state-hover"); } function processRegister(xml) { if($("status",xml).text() == "1") { result.removeClass('error'); result.addClass('success'); result.text($("message",xml).text()); tips.text(''); tips.removeClass('process error'); $('#dialog').dialog('close'); } else if($("status",xml).text() == "2") { updateTips($("message",xml).text()); } else { updateTips('Error has occured. Please try again later.'); } fixSubmit(); } function processRegisterError(error) { updateTips('Error has occured ('+error+'). Please try again later.'); fixSubmit(); } function updateTips(t) { tips.removeClass('process'); tips.addClass('error'); tips.text(t); } function processTips(t) { tips.removeClass('error'); tips.addClass('process'); tips.text(t); } function checkLength(o,n,min,max) { if ( o.val().length > max || o.val().length < min ) { o.addClass('ui-state-error'); updateTips("Length of " + n + " must be between "+min+" and "+max+"."); return false; } else { return true; } } function checkRegexp(o,regexp,n) { if ( !( regexp.test( o.val() ) ) ) { o.addClass('ui-state-error'); updateTips(n); return false; } else { return true; } } $("#dialog").dialog({ bgiframe: true, autoOpen: false, resizable: false, modal: true, buttons: { 'Register an account': function() { var bValid = true; allFields.removeClass('ui-state-error'); bValid = bValid && checkLength(name,"username",3,16); bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Username may consist of a-z, 0-9, underscores, must begin with a letter."); bValid = bValid && checkLength(email,"e-mail address",6,120); bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Invalid e-mail address: eg. hello@ZeeOrbit.com"); bValid = bValid && checkLength(password,"password",5,16); bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9"); bValid = bValid && checkLength(cpassword,"confirm password",5,16); bValid = bValid && checkRegexp(cpassword,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9"); if(bValid && password.val() != cpassword.val()) { bValid = false; password.addClass('ui-state-error'); cpassword.addClass('ui-state-error'); updateTips("Passwords do not match"); } if (bValid) { var submitButton = $(".ui-dialog > .ui-dialog-buttonpane > button:first"); submitButton.attr('disabled','disabled'); submitButton.text('Please wait...'); processTips("Submitting data to the server."); $.ajax({ method: "post", url: "/ajax.php", dataType: "xml", cache: false, timeout: 15000, data: { 'username': name.val(), 'email': email.val(), 'cpassword': cpassword.val(), 'password': password.val(), 'country': country.val(), 'action': 'register' }, success: function(xml){ processRegister(xml); }, error: function (XMLHttpRequest, textStatus, errorThrown) { processRegisterError(textStatus); } }); } }, Cancel: function() { $(this).dialog('close'); } }, close: function() { allFields.val('').removeClass('ui-state-error'); tips.text(''); tips.removeClass('error process'); fixSubmit(); } }); $('#register').click(function() { $('#dialog').dialog('open'); }) .hover( function(){ $(this).addClass("ui-state-hover"); }, function(){ $(this).removeClass("ui-state-hover"); } ).mousedown(function(){ $(this).addClass("ui-state-active"); }) .mouseup(function(){ $(this).removeClass("ui-state-active"); }); });