Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. //Ok. So this part is for fetching the error message, in case the user did something wrong.
  2. //This wasn't written by me. https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript#23740549
  3. var errorMSG = new XMLHttpRequest(); //New request object
  4. errorMSG.onload = function() {
  5. document.getElementById("error").innerHTML = this.responseText;
  6. };
  7. errorMSG.open("get", "submit.php", true);
  8. // ^ Don't block the rest of the execution.
  9. // Don't wait until the request finishes to
  10. // continue.
  11. errorMSG.send();
  12.  
  13.  
  14. //Function to show an error in the "#error" element
  15. function throwError(err){
  16. document.getElementById("error").innerHTML = "Error: " + err;
  17. }
  18.  
  19.  
  20. //This is for registering - there's some functions I can do on the client side, and I prefer client-side.
  21. function register(){
  22. var username = document.getElementById("username").value;
  23. var password = document.getElementById("password").value;
  24. var confirmpassword = document.getElementById("confirm-password").value;
  25. var emailaddress = document.getElementById("email-address").value;
  26. var usernameblacklist = ["thijs365", "toomuchram", "thijs", "tmr", "tristan", "wificable", "pcnerd512", "admin", "siteadmin"];
  27.  
  28. //Check if any of the values is empty.
  29. if(username === null || username === "" || username === undefined){
  30. throwError("Users should be able to identify you. That's a bit difficult without a username, right?");
  31. return 1;
  32. }
  33. if(password === null || password === "" || password === undefined){
  34. throwError("Not going to lie, that's some strong authentication.");
  35. return 1;
  36. }
  37. if(emailaddress === null || emailaddress === "" || emailaddress === undefined){
  38. throwError("Don't worry, I'm not like Facebook.");
  39. return 1;
  40. }
  41.  
  42.  
  43.  
  44. //Always check if the two passwords are the same.
  45. if(password !== confirmpassword){
  46. throwError("The two password aren't the same");
  47. return 1;
  48. }
  49. //Then check if the password's on a blacklist - because I'm a dickhead.
  50. //Create a temporary variable that holds a lowercased version of the username - I don't want someone to create tHijs365!
  51. var lowercaseusername = username.toLowerCase();
  52. //Also strip it from punctuatio
  53. lowercaseusername = lowercaseusername.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
  54. //And fix it up
  55. lowercaseusername = lowercaseusername.replace(/\s{2,}/g," ");
  56. //source: https://stackoverflow.com/questions/4328500/how-can-i-strip-all-punctuation-from-a-string-in-javascript-using-regex#4328722
  57.  
  58. for(var i=0; i<usernameblacklist.length; i++){
  59. if(lowercaseusername == usernameblacklist[i]){
  60. throwError("This username is on the blacklist");
  61. return 1;
  62. }
  63. }
  64.  
  65. //I steal from SO all the time.
  66. //https://stackoverflow.com/questions/692196/post-request-javascript#25422754
  67.  
  68. var params = "username=" + username + "&password=" + password + "&emailaddress=" + emailaddress;
  69. var accountrequest = new XMLHttpRequest();
  70. accountrequest.open("POST", "submit.php", true);
  71.  
  72. //Send the proper header information along with the request
  73. accountrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  74. accountrequest.send(params);
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement