Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. // Get the json file and store
  2. function loadJSON(callback) {
  3. var xobj = new XMLHttpRequest();
  4. xobj.overrideMimeType("application/json");
  5. xobj.open('GET', 'js/rules.json');
  6. xobj.onreadystatechange = function () {
  7. if (xobj.readyState == 4 && xobj.status == "200") {
  8. // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
  9. callback(xobj.responseText);
  10. }
  11. };
  12. xobj.send(null);
  13. }
  14.  
  15. // Load json...
  16. loadJSON(response);
  17.  
  18. // Create global vars...
  19. var lookup = [], errors = [], i, e, id, lookupId, minLength;
  20.  
  21. function response(responseData) {
  22. // Create objects from json data
  23. var rulesSet = JSON.parse(responseData);
  24.  
  25. // Loop through objects
  26. for (i = 0; i < rulesSet.length; i++) {
  27. // Create a lookup for each object that can be used later
  28. lookup[rulesSet[i].id] = rulesSet[i];
  29. }
  30.  
  31. // Loop through form elements and store id's
  32. var elements = document.getElementsByTagName('input');
  33. for (e = 0; e < elements.length; e++) {
  34. id = elements[e].getAttribute('id');
  35.  
  36. // Validate the form
  37. function validate(e) {
  38. lookupId = lookup[id].rules; var rules;
  39. // Loop through rules of the matched ID's
  40. for (rules of lookupId){
  41.  
  42. // Check if there is a min length rule
  43. if(rules.name === 'min_length') {
  44. minLength = rules.value.toString();
  45. // Check if the rule is valid (is a number)
  46. if(isNaN(minLength) || minLength.length === 0){
  47. // Log an error somewhere here
  48. // Run the minLength function and report an error if it fails validation
  49. } else if(!checkMinLength(minLength, id)) {
  50. errors[errors.length] = id + " - You must enter more than " + minLength + " characters";
  51. }
  52. }
  53.  
  54. }
  55.  
  56.  
  57. // If there are errors, report them
  58. if (errors.length > 0) {
  59. reportErrors(errors);
  60. e.preventDefault();
  61. }
  62.  
  63. }
  64.  
  65. }
  66.  
  67. // Check the length of the field
  68. function checkMinLength(minLength, id){
  69. var val = document.getElementById(id).value;
  70. if(val < minLength){
  71. return false;
  72. }
  73. return true;
  74. }
  75.  
  76. // Error reporting
  77. function reportErrors(errors){
  78. for (var i=0; i<errors.length; i++) {
  79. var msg = errors[i];
  80. }
  81. console.log(msg);
  82. }
  83.  
  84.  
  85. $('#email-submit').on('click',function(e){
  86. validate(e);
  87. });
  88.  
  89. }
  90.  
  91. [
  92. {
  93. "id": "search",
  94. "rules": [
  95. {
  96. "name": "min_length",
  97. "value": "5"
  98. },
  99. {
  100. "name": "email"
  101. }
  102. ]
  103. },
  104. {
  105. "id": "phone-number",
  106. "rules": [
  107. {
  108. "name": "min_length",
  109. "value": 8
  110. }
  111. ]
  112. },
  113. {
  114. "id": "surname",
  115. "rules": [
  116. {
  117. "name": "min_length",
  118. "value": 10
  119. }
  120. ]
  121. }
  122. ]
  123.  
  124. <form action="index.html" name="searchForm" id="search-form">
  125. <label for="search">Email</label>
  126. <input type="text" id="search" name="email" placeholder="Enter email">
  127. <input type="text" id="phone-number" name="name" placeholder="Enter name">
  128. <button type="submit" id="email-submit">Submit</button>
  129. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement