Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Get the json file and store
- function loadJSON(callback) {
- var xobj = new XMLHttpRequest();
- xobj.overrideMimeType("application/json");
- xobj.open('GET', 'js/rules.json');
- xobj.onreadystatechange = function () {
- if (xobj.readyState == 4 && xobj.status == "200") {
- // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
- callback(xobj.responseText);
- }
- };
- xobj.send(null);
- }
- // Load json...
- loadJSON(response);
- // Create global vars...
- var lookup = [], errors = [], i, e, id, lookupId, minLength;
- function response(responseData) {
- // Create objects from json data
- var rulesSet = JSON.parse(responseData);
- // Loop through objects
- for (i = 0; i < rulesSet.length; i++) {
- // Create a lookup for each object that can be used later
- lookup[rulesSet[i].id] = rulesSet[i];
- }
- // Loop through form elements and store id's
- var elements = document.getElementsByTagName('input');
- for (e = 0; e < elements.length; e++) {
- id = elements[e].getAttribute('id');
- // Validate the form
- function validate(e) {
- lookupId = lookup[id].rules; var rules;
- // Loop through rules of the matched ID's
- for (rules of lookupId){
- // Check if there is a min length rule
- if(rules.name === 'min_length') {
- minLength = rules.value.toString();
- // Check if the rule is valid (is a number)
- if(isNaN(minLength) || minLength.length === 0){
- // Log an error somewhere here
- // Run the minLength function and report an error if it fails validation
- } else if(!checkMinLength(minLength, id)) {
- errors[errors.length] = id + " - You must enter more than " + minLength + " characters";
- }
- }
- }
- // If there are errors, report them
- if (errors.length > 0) {
- reportErrors(errors);
- e.preventDefault();
- }
- }
- }
- // Check the length of the field
- function checkMinLength(minLength, id){
- var val = document.getElementById(id).value;
- if(val < minLength){
- return false;
- }
- return true;
- }
- // Error reporting
- function reportErrors(errors){
- for (var i=0; i<errors.length; i++) {
- var msg = errors[i];
- }
- console.log(msg);
- }
- $('#email-submit').on('click',function(e){
- validate(e);
- });
- }
- [
- {
- "id": "search",
- "rules": [
- {
- "name": "min_length",
- "value": "5"
- },
- {
- "name": "email"
- }
- ]
- },
- {
- "id": "phone-number",
- "rules": [
- {
- "name": "min_length",
- "value": 8
- }
- ]
- },
- {
- "id": "surname",
- "rules": [
- {
- "name": "min_length",
- "value": 10
- }
- ]
- }
- ]
- <form action="index.html" name="searchForm" id="search-form">
- <label for="search">Email</label>
- <input type="text" id="search" name="email" placeholder="Enter email">
- <input type="text" id="phone-number" name="name" placeholder="Enter name">
- <button type="submit" id="email-submit">Submit</button>
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement