Advertisement
Guest User

Untitled

a guest
Jul 5th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.12 KB | None | 0 0
  1. var reg = {
  2. //The Fields that we need to validate
  3. fields: {
  4. "First Name": { name: "s_firstname", minChar: "3", regEx: /^.{3,}$/ },
  5. "Last Name": { name: "s_lastname", minChar: "3", regEx: /^.{3,}$/ },
  6. "Username": { name: "s_username", minChar: "3", maxChar: "20", regEx: /^[A-Za-z0-9_-]{3,20}$/ },
  7. "Password": { name: "s_password", minChar: "6", maxChar: "16", regEx: /^.{6,16}$/ },
  8. "Email": { name: "s_email", regEx: /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i },
  9. "Birthday Month": { name: "bmonth" },
  10. "Birthday Day": { name: "bday" },
  11. "Birthday Year": { name: "byear" }
  12. },
  13.  
  14. // "Username": {name: "s_username", minChar: "3", maxChar: "20", regEx: /^[A-Za-z0-9_-]{3,20}$/ },
  15. // "Password": { name: "s_password", minChar: "6", maxChar: "16", regEx: /^.{6,16}$/ },
  16.  
  17.  
  18. //Populate reg.fields[""].ele
  19. populate: function() {
  20. for(key in reg.fields) {
  21. reg.fields[key].ele = $("*[name=" + reg.fields[key].name + "]");
  22. }
  23. },
  24.  
  25. //To loop through all the fields and make sure it is not empty
  26. validate: function()
  27. {
  28. $("#errorTxt").html("");
  29. $("#error_message").hide(0);
  30. for(key in reg.fields) {
  31. //Get current field object from loop
  32. var fieldObj = reg.fields[key];
  33.  
  34. //Get the current field from the loop
  35. var field = fieldObj.ele;
  36.  
  37. //Set the trimmed value
  38. var fieldValue = jQuery.trim(field.attr("value"));
  39.  
  40. //It's a text field
  41. if(field.attr("type") == "text" || field.attr("type") == "password") {
  42. //Check if empty value
  43. if(fieldValue == "")
  44. {
  45. //It's empty, let's show a error to the user
  46. reg.error(key + " is empty. Please fill it in.", field);
  47.  
  48. //Break out of loop and return false
  49. return false;
  50.  
  51. } else if (!fieldObj.regEx.test(fieldValue)) { //Check against regExp
  52. //Check to see if there is a min or max char specified
  53. if(fieldObj.minChar || fieldObj.maxChar)
  54. {
  55. //Build char limit str
  56. var charStr = (fieldObj.minChar) ? " must be " + fieldObj.minChar + " chars" : "";
  57. charStr += (charStr == "" && fieldObj.maxChar) ? " must be " + fieldObj.maxChar + " chars" : (fieldObj.maxChar) ? " to " + fieldObj.maxChar + " characters" : "";
  58.  
  59. //Show char error
  60. reg.error(key + charStr + ".", field);
  61.  
  62. //Break out of loop and return false
  63. return false;
  64.  
  65. }
  66. else { //Assume the problem is not a size issue but a validation issue
  67. reg.error(key + " is not valid.", field);
  68.  
  69. //Break out of loop and return false
  70. return false;
  71. }
  72. }
  73. } else { //It's not a text field. We'll assume it's a select box
  74. if(fieldValue == "-1") {
  75. reg.error("Please select your " + key + ".", field);
  76.  
  77. //Break out of loop and return false
  78. return false;
  79. }
  80. }
  81. }
  82.  
  83. //Most of the checks are now passed, let's do one of the final steps; determine how old the user is
  84. //The current date obj
  85. var currentDate = new Date();
  86.  
  87. //Birthday month
  88. var bDayMonth = reg.fields["Birthday Month"].ele.attr("value");
  89.  
  90. //Birthdate
  91. var bDate = reg.fields["Birthday Day"].ele.attr("value");
  92.  
  93. //Birthday Year
  94. var bDayYear = reg.fields["Birthday Year"].ele.attr("value");
  95.  
  96. //Birthday date object
  97. var bDayObj = new Date(bDayMonth + ", " + bDate + ", " + bDayYear);
  98.  
  99. //Let's minus the bDay date obj from the current date obj, and divide it down to years
  100. var userYearAge = (currentDate - bDayObj)/1000/60/60/24/365;
  101.  
  102. //Check if the user is over 13
  103. if (userYearAge < 14) {
  104. reg.error("Sorry, you are not eligible to register on -.", reg.fields["Birthday Month"].ele);
  105.  
  106. //Break out of loop and return false
  107. return false;
  108. } else { //All checks have passed except the "In Use" username/email check, which is the final check
  109. reg.checkUse();
  110. }
  111.  
  112. return true;
  113. },
  114.  
  115. //Check if email or username is in use
  116. checkUse: function()
  117. {
  118. var usernameValue = reg.fields["Username"].ele.attr("value");
  119. var emailValue = reg.fields["Email"].ele.attr("value");
  120. var firstnameValue = reg.fields["First Name"].ele.attr("value");
  121. var lastnameValue = reg.fields["Last Name"].ele.attr("value");
  122. var passwordValue = reg.fields["Password"].ele.attr("value");
  123. var bDayMonth = reg.fields["Birthday Month"].ele.attr("value");
  124. var bDayDay = reg.fields["Birthday Day"].ele.attr("value");
  125. var bDayYear = reg.fields["Birthday Year"].ele.attr("value");
  126.  
  127. $.ajax({
  128. type: "POST",
  129. url: "/home/ax",
  130. dataType: "text",
  131. data: "action=registeruser&username=" + usernameValue + "&email=" + emailValue + "&firstname=" +
  132. firstnameValue + "&lastname=" + lastnameValue + "&password=" + passwordValue +
  133. "&bmonth=" + bDayMonth + "&bday=" + bDayDay + "&byear=" + bDayYear,
  134. success: function (inUse) {
  135. var usernameEle = reg.fields["Username"].ele;
  136. var emailEle = reg.fields["Email"].ele;
  137.  
  138. switch(inUse)
  139. {
  140. case "sweet":
  141. reg.register();
  142. break;
  143. case "username":
  144. reg.error("The username you picked is already in use.", usernameEle);
  145. break;
  146. case "email":
  147. reg.error("The email you used is already registered.", emailEle);
  148. break;
  149. case "both":
  150. reg.error("The username AND email you used is already registered.", usernameEle);
  151. break;
  152. case "validate":
  153. reg.error("Unable to validate. Please check your information.", usernameEle);
  154. break;
  155. }
  156. }
  157. });
  158. },
  159.  
  160. register: function()
  161. {
  162. location.href = '/filldetails';
  163. },
  164.  
  165. //To show the error to the user
  166. error: function(str, field)
  167. {
  168. $("#errorTxt").html(str);
  169. $("#error_message").show(400);
  170. field.focus();
  171. }
  172. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement