Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Ok. So this part is for fetching the error message, in case the user did something wrong.
- //This wasn't written by me. https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript#23740549
- var errorMSG = new XMLHttpRequest(); //New request object
- errorMSG.onload = function() {
- document.getElementById("error").innerHTML = this.responseText;
- };
- errorMSG.open("get", "submit.php", true);
- // ^ Don't block the rest of the execution.
- // Don't wait until the request finishes to
- // continue.
- errorMSG.send();
- //Function to show an error in the "#error" element
- function throwError(err){
- document.getElementById("error").innerHTML = "Error: " + err;
- }
- //This is for registering - there's some functions I can do on the client side, and I prefer client-side.
- function register(){
- var username = document.getElementById("username").value;
- var password = document.getElementById("password").value;
- var confirmpassword = document.getElementById("confirm-password").value;
- var emailaddress = document.getElementById("email-address").value;
- var usernameblacklist = ["thijs365", "toomuchram", "thijs", "tmr", "tristan", "wificable", "pcnerd512", "admin", "siteadmin"];
- //Check if any of the values is empty.
- if(username === null || username === "" || username === undefined){
- throwError("Users should be able to identify you. That's a bit difficult without a username, right?");
- return 1;
- }
- if(password === null || password === "" || password === undefined){
- throwError("Not going to lie, that's some strong authentication.");
- return 1;
- }
- if(emailaddress === null || emailaddress === "" || emailaddress === undefined){
- throwError("Don't worry, I'm not like Facebook.");
- return 1;
- }
- //Always check if the two passwords are the same.
- if(password !== confirmpassword){
- throwError("The two password aren't the same");
- return 1;
- }
- //Then check if the password's on a blacklist - because I'm a dickhead.
- //Create a temporary variable that holds a lowercased version of the username - I don't want someone to create tHijs365!
- var lowercaseusername = username.toLowerCase();
- //Also strip it from punctuatio
- lowercaseusername = lowercaseusername.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
- //And fix it up
- lowercaseusername = lowercaseusername.replace(/\s{2,}/g," ");
- //source: https://stackoverflow.com/questions/4328500/how-can-i-strip-all-punctuation-from-a-string-in-javascript-using-regex#4328722
- for(var i=0; i<usernameblacklist.length; i++){
- if(lowercaseusername == usernameblacklist[i]){
- throwError("This username is on the blacklist");
- return 1;
- }
- }
- //I steal from SO all the time.
- //https://stackoverflow.com/questions/692196/post-request-javascript#25422754
- var params = "username=" + username + "&password=" + password + "&emailaddress=" + emailaddress;
- var accountrequest = new XMLHttpRequest();
- accountrequest.open("POST", "submit.php", true);
- //Send the proper header information along with the request
- accountrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- accountrequest.send(params);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement