form-validation.js
272 lines
| 1 | /** |
| 2 | * Submission form validation |
| 3 | */ |
| 4 | |
| 5 | var strongValidation = { |
| 6 | |
| 7 | defaults: { |
| 8 | ajaxUrl: '', |
| 9 | display: { |
| 10 | successMessage: false |
| 11 | }, |
| 12 | scroll: { |
| 13 | onError: true, |
| 14 | onErrorOffset: 100, |
| 15 | onSuccess: true, |
| 16 | onSuccessOffset: 100 |
| 17 | }, |
| 18 | fields: {}, |
| 19 | }, |
| 20 | |
| 21 | settings: {}, |
| 22 | |
| 23 | setOpts: function (options) { |
| 24 | this.settings = jQuery.extend({}, this.defaults, options); |
| 25 | }, |
| 26 | |
| 27 | rules: {}, |
| 28 | |
| 29 | /** |
| 30 | * Add custom validation rule to star-rating pseudo elements. |
| 31 | */ |
| 32 | setRules: function () { |
| 33 | for (var i = 0; i < this.settings.fields.length; i++) { |
| 34 | |
| 35 | if ('rating' === this.settings.fields[i].type) { |
| 36 | if (1 === this.settings.fields[i].required) { |
| 37 | this.rules[this.settings.fields[i].name] = {ratingRequired: true}; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | } |
| 42 | }, |
| 43 | |
| 44 | /** |
| 45 | * Initialize. |
| 46 | */ |
| 47 | init: function () { |
| 48 | |
| 49 | var strongForm = {}; |
| 50 | if (typeof window['strongForm'] !== 'undefined') { |
| 51 | strongForm = window['strongForm']; |
| 52 | } |
| 53 | this.setOpts(strongForm); |
| 54 | |
| 55 | if (this.settings.display.successMessage) { |
| 56 | |
| 57 | this.scrollOnSuccess(); |
| 58 | |
| 59 | } else { |
| 60 | |
| 61 | this.setRules(); |
| 62 | this.changeEvents(); |
| 63 | this.customValidators(); |
| 64 | this.validateForm(); |
| 65 | |
| 66 | } |
| 67 | |
| 68 | }, |
| 69 | |
| 70 | changeEvents: function () { |
| 71 | |
| 72 | // Trim blanks |
| 73 | jQuery('input[type="text"], input[type="url"], input[type="email"], textarea', '#wpmtst-submission-form').on('change blur', function (e) { |
| 74 | e.target.value = e.target.value.trim(); |
| 75 | }); |
| 76 | |
| 77 | // Add protocol if missing |
| 78 | // Thanks http://stackoverflow.com/a/36429927/51600 |
| 79 | jQuery('input[type=url]').change(function () { |
| 80 | if (this.value.length && !/^https*:\/\//.test(this.value)) { |
| 81 | this.value = 'https://' + this.value; |
| 82 | } |
| 83 | }); |
| 84 | |
| 85 | // Star ratings |
| 86 | var ratings = document.getElementsByClassName('strong-rating'); |
| 87 | for (var i = 0; i < ratings.length; i++) { |
| 88 | // Handle keystrokes |
| 89 | ratings[i].addEventListener('click', this.handleRadioEvent, true); |
| 90 | ratings[i].addEventListener('keyup', this.handleRadioEvent, true); |
| 91 | // Validate on change |
| 92 | ratings[i].addEventListener('change', function () { jQuery(this).valid(); }, true); |
| 93 | } |
| 94 | |
| 95 | }, |
| 96 | |
| 97 | handleRadioEvent: function (e) { |
| 98 | // If key 0-5 fired the event, trigger click on that star (including hidden zero). |
| 99 | if (e.keyCode >= 48 && e.keyCode <= 53) { |
| 100 | var key = e.keyCode - 48; |
| 101 | jQuery(this).find('input[type="radio"][value=' + key + ']').click(); |
| 102 | } |
| 103 | }, |
| 104 | |
| 105 | customValidators: function () { |
| 106 | /** |
| 107 | * Only use elements that can legitimately have a 'name' attribute: |
| 108 | * <button>, <form>, <fieldset>, <iframe>, <input>, <keygen>, <object>, |
| 109 | * <output>, <select>, <textarea>, <map>, <meta>, <param> |
| 110 | * |
| 111 | * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes |
| 112 | * |
| 113 | * jQuery Validate v1.16.0 |
| 114 | * As of 6/10/2017 |
| 115 | */ |
| 116 | jQuery.validator.addMethod('ratingRequired', function (value, element) { |
| 117 | return jQuery(element).find('input:checked').val() > 0; |
| 118 | }, jQuery.validator.messages.required); |
| 119 | }, |
| 120 | |
| 121 | validateForm: function () { |
| 122 | |
| 123 | /** |
| 124 | * Validate the form |
| 125 | */ |
| 126 | var theForm = jQuery('#wpmtst-submission-form'); |
| 127 | |
| 128 | theForm.validate({ |
| 129 | |
| 130 | onfocusout: false, |
| 131 | |
| 132 | focusInvalid: false, |
| 133 | |
| 134 | invalidHandler: function (form, validator) { |
| 135 | // Focus first invalid input |
| 136 | var errors = validator.numberOfInvalids(); |
| 137 | if (errors) { |
| 138 | if (strongValidation.settings.scroll.onError) { |
| 139 | if (typeof validator.errorList[0] !== 'undefined') { |
| 140 | var firstError = jQuery(validator.errorList[0].element); |
| 141 | var fieldOffset = firstError.closest('.form-field').offset(); |
| 142 | var scrollTop = fieldOffset.top - strongValidation.settings.scroll.onErrorOffset; |
| 143 | jQuery('html, body').animate({scrollTop: scrollTop}, 800, function () { firstError.focus(); }); |
| 144 | } |
| 145 | } else { |
| 146 | validator.errorList[0].element.focus(); |
| 147 | } |
| 148 | } |
| 149 | }, |
| 150 | |
| 151 | submitHandler: function (form) { |
| 152 | |
| 153 | strongValidation.disableForm(); |
| 154 | |
| 155 | // If Ajax |
| 156 | if (strongValidation.settings.ajaxUrl !== '') { |
| 157 | |
| 158 | window.onbeforeunload = function() { |
| 159 | return "Please wait while the form is submitted."; |
| 160 | } |
| 161 | |
| 162 | var formOptions = { |
| 163 | url: strongValidation.settings.ajaxUrl, |
| 164 | data: { |
| 165 | action: 'wpmtst_form2' |
| 166 | }, |
| 167 | success: strongValidation.showResponse, |
| 168 | }; |
| 169 | jQuery(form).ajaxSubmit(formOptions); |
| 170 | |
| 171 | } else { |
| 172 | |
| 173 | form.submit(); |
| 174 | |
| 175 | } |
| 176 | }, |
| 177 | |
| 178 | /* Normalizer not working */ |
| 179 | // normalizer: function( value ) { |
| 180 | // return jQuery.trim( value ) |
| 181 | // }, |
| 182 | |
| 183 | rules: strongValidation.rules, |
| 184 | |
| 185 | errorPlacement: function (error, element) { |
| 186 | error.appendTo(element.closest('div.form-field')); |
| 187 | }, |
| 188 | |
| 189 | highlight: function (element, errorClass, validClass) { |
| 190 | if (element.type === 'checkbox') { |
| 191 | jQuery(element).closest('.field-wrap').addClass(errorClass).removeClass(validClass); |
| 192 | } else if ('rating' === jQuery(element).data('fieldType')) { |
| 193 | jQuery(element).closest('.field-wrap').addClass(errorClass).removeClass(validClass); |
| 194 | } else { |
| 195 | jQuery(element).addClass(errorClass).removeClass(validClass); |
| 196 | } |
| 197 | }, |
| 198 | |
| 199 | unhighlight: function (element, errorClass, validClass) { |
| 200 | if (element.type === 'checkbox') { |
| 201 | jQuery(element).closest('.field-wrap').removeClass(errorClass).addClass(validClass); |
| 202 | } else if ('rating' === jQuery(element).data('fieldType')) { |
| 203 | jQuery(element).closest('.field-wrap').removeClass(errorClass).addClass(validClass); |
| 204 | } else { |
| 205 | jQuery(element).removeClass(errorClass).addClass(validClass); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | }); |
| 210 | |
| 211 | }, |
| 212 | |
| 213 | /** |
| 214 | * Display message/errors upon Ajax submission |
| 215 | * |
| 216 | * @param response |
| 217 | */ |
| 218 | showResponse: function (response) { |
| 219 | window.onbeforeunload = null; |
| 220 | strongValidation.enableForm(); |
| 221 | var obj = JSON.parse(response); |
| 222 | if (obj.success) { |
| 223 | jQuery('#wpmtst-form').html(obj.message); |
| 224 | strongValidation.scrollOnSuccess(); |
| 225 | } else { |
| 226 | for (var key in obj.errors) { |
| 227 | if (obj.errors.hasOwnProperty(key)) { |
| 228 | jQuery('div.wpmtst-' + key) |
| 229 | .find('span.error') |
| 230 | .remove() |
| 231 | .end() |
| 232 | .append('<span class="error">' + obj.errors[key] + '</span>'); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | }, |
| 237 | |
| 238 | /** |
| 239 | * Scroll to success message |
| 240 | */ |
| 241 | scrollOnSuccess: function () { |
| 242 | if (strongValidation.settings.scroll.onSuccess) { |
| 243 | var containerOffset, scrollTop; |
| 244 | containerOffset = jQuery('.testimonial-success').offset(); |
| 245 | if (containerOffset) { |
| 246 | scrollTop = containerOffset.top - strongValidation.settings.scroll.onSuccessOffset; |
| 247 | // is WordPress admin bar showing? |
| 248 | if (jQuery('#wpadminbar').length) { |
| 249 | scrollTop -= 32; |
| 250 | } |
| 251 | jQuery('html, body').animate({scrollTop: scrollTop}, 800); |
| 252 | } |
| 253 | } |
| 254 | }, |
| 255 | |
| 256 | /** |
| 257 | * Show overlay during form submission. |
| 258 | */ |
| 259 | disableForm: function () { |
| 260 | jQuery('.strong-form-wait').show(); |
| 261 | jQuery('#wpmtst_submit_testimonial').prop('disabled',true); |
| 262 | }, |
| 263 | |
| 264 | /** |
| 265 | * Hide overlay after form submission. |
| 266 | */ |
| 267 | enableForm: function () { |
| 268 | jQuery('.strong-form-wait').hide(); |
| 269 | jQuery('#wpmtst_submit_testimonial').prop('disabled',false); |
| 270 | } |
| 271 | }; |
| 272 |