simple-job-board-public.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. * Simple Job Board Core Front-end JS File - V 1.4.0
  3. *
  4. * @author PressTigers <support@presstigers.com>, 2016
  5. *
  6. * Actions List
  7. * - Job Application Submission Callbacks
  8. * - Date Picker Initialization
  9. * - Validate Email
  10. * - Initialize TelInput Plugin
  11. * - Validate Phone Number
  12. * - Allowable Uploaded File's Extensions
  13. * - Validate Required Inputs ( Attachment, Phone & Email )
  14. * - Checkbox Group Required Attribute Callbacks
  15. * - Custom Styling of File Upload Button
  16. */
  17. (function ($) {
  18. 'use strict';
  19. $(document).ready(function () {
  20. var jobpost_submit_button = $('.app-submit');
  21. $(".jobpost-form").on("submit", function (event) {
  22. var jobpost_form_status = $('#jobpost_form_status');
  23. var datastring = new FormData(document.getElementById("sjb-application-form"));
  24. /**
  25. * Application Form Submit -> Validate Email & Phone
  26. * @since 2.2.0
  27. */
  28. var is_valid_email = sjb_is_valid_input(event, "email", "sjb-email-address");
  29. var is_valid_phone = sjb_is_valid_input(event, "phone", "sjb-phone-number");
  30. var is_attachment = sjb_is_attachment(event);
  31. /* Stop Form Submission on Invalid Phone, Email & File Attachement */
  32. if ( !is_valid_email || !is_valid_phone || !is_attachment ) {
  33. return false;
  34. }
  35. $.ajax({
  36. url: application_form.ajaxurl,
  37. type: 'POST',
  38. dataType: 'json',
  39. data: datastring,
  40. async: false,
  41. cache: false,
  42. contentType: false,
  43. processData: false,
  44. beforeSend: function () {
  45. jobpost_form_status.html('Submitting.....');
  46. jobpost_submit_button.attr('disabled', 'diabled');
  47. },
  48. success: function ( response ) {
  49. if ( response['success'] == true ) {
  50. $('.jobpost-form').slideUp();
  51. /* Translation Ready String Through Script Locaization */
  52. jobpost_form_status.html(response['success_alert']);
  53. }
  54. if ( response['success'] == false ) {
  55. /* Translation Ready String Through Script Locaization */
  56. jobpost_form_status.html( response['error'] + ' ' + application_form.jquery_alerts['application_not_submitted'] + '</div>' );
  57. jobpost_submit_button.removeAttr( 'disabled' );
  58. }
  59. }
  60. });
  61. return false;
  62. });
  63. /* Date Picker */
  64. $('.sjb-datepicker').datepicker({
  65. dateFormat: 'dd-mm-yy',
  66. changeMonth: true,
  67. changeYear: true,
  68. yearRange: '-100:+50',
  69. });
  70. /**
  71. * Application Form -> On Input Email Validation
  72. *
  73. * @since 2.2.0
  74. */
  75. $('.sjb-email-address').on('input', function () {
  76. var input = $(this);
  77. var re = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
  78. var is_email = re.test(input.val());
  79. var error_element = $(this).next();
  80. if (is_email) {
  81. input.removeClass("invalid").addClass("valid");
  82. error_element.hide();
  83. } else {
  84. input.removeClass("valid").addClass("invalid");
  85. }
  86. });
  87. /**
  88. * Initialize TelInput Plugin
  89. *
  90. * @since 2.2.0
  91. */
  92. if ($('.sjb-phone-number').length) {
  93. var telInput_id = $('.sjb-phone-number').map(function () {
  94. return this.id;
  95. }).get();
  96. for (var input_ID in telInput_id) {
  97. var telInput = $('#' + telInput_id[input_ID]);
  98. telInput.intlTelInput({
  99. initialCountry: "auto",
  100. geoIpLookup: function (callback) {
  101. $.get('https://ipinfo.io', function () {
  102. }, "jsonp").always(function (resp) {
  103. var countryCode = (resp && resp.country) ? resp.country : "";
  104. callback(countryCode);
  105. });
  106. },
  107. });
  108. }
  109. }
  110. /**
  111. * Application Form -> Phone Number Validation
  112. *
  113. * @since 2.2.0
  114. */
  115. $('.sjb-phone-number').on('input', function () {
  116. var telInput = $(this);
  117. var telInput_id = $(this).attr('id');
  118. var error_element = $("#" + telInput_id + "-invalid-phone");
  119. error_element.hide();
  120. // Validate Phone Number
  121. if ($.trim(telInput.val())) {
  122. if (telInput.intlTelInput("isValidNumber")) {
  123. telInput.removeClass("invalid").addClass("valid");
  124. error_element.hide();
  125. } else {
  126. telInput.removeClass("valid").addClass("invalid");
  127. }
  128. }
  129. });
  130. /**
  131. * Check for Allowable Extensions of Uploaded File
  132. *
  133. * @since 2.3.0
  134. */
  135. $('.sjb-attachment').on('change', function () {
  136. var input = $(this);
  137. var file = $("#" + $(this).attr("id"));
  138. var error_element = file.parent().next("span");
  139. error_element.text('');
  140. error_element.hide();
  141. // Validate on File Attachment
  142. if ( 0 != file.get(0).files.length ) {
  143. /**
  144. * Uploded File Extensions Checks
  145. * Get Uploded File Ext
  146. */
  147. var file_ext = file.val().split('.').pop().toLowerCase();
  148. // All Allowed File Extensions
  149. var allowed_file_exts = application_form.allowed_extensions;
  150. // Settings File Extensions && Getting value From Script Localization
  151. var settings_file_exts = application_form.setting_extensions;
  152. var selected_file_exts = (('yes' === application_form.all_extensions_check) || null == settings_file_exts) ? allowed_file_exts : settings_file_exts;
  153. // File Extension Validation
  154. if ($.inArray(file_ext, selected_file_exts) > -1) {
  155. jobpost_submit_button.attr( 'disabled', false );
  156. input.removeClass("invalid").addClass("valid");
  157. } else {
  158. /* Translation Ready String Through Script Locaization */
  159. error_element.text(application_form.jquery_alerts['invalid_extension']);
  160. error_element.show();
  161. input.removeClass("valid").addClass("invalid");
  162. }
  163. }
  164. });
  165. /**
  166. * Stop Form Submission -> On Required Attachments
  167. *
  168. * @since 2.3.0
  169. */
  170. function sjb_is_attachment( event ) {
  171. var error_free = true;
  172. $(".sjb-attachment").each(function () {
  173. var element = $("#" + $(this).attr("id"));
  174. var valid = element.hasClass("valid");
  175. var is_required_class = element.hasClass("sjb-not-required");
  176. // Set Error Indicator on Invalid Attachment
  177. if (!valid) {
  178. if (!(is_required_class && 0 === element.get(0).files.length)) {
  179. error_free = false;
  180. }
  181. }
  182. // Stop Form Submission
  183. if (!error_free) {
  184. event.preventDefault();
  185. }
  186. });
  187. return error_free;
  188. }
  189. /**
  190. * Stop Form Submission -> On Invalid Email/Phone
  191. *
  192. * @since 2.2.0
  193. */
  194. function sjb_is_valid_input(event, input_type, input_class) {
  195. var jobpost_form_inputs = $("." + input_class).serializeArray();
  196. var error_free = true;
  197. for (var i in jobpost_form_inputs) {
  198. var element = $("#" + jobpost_form_inputs[i]['name']);
  199. var valid = element.hasClass("valid");
  200. var is_required_class = element.hasClass("sjb-not-required");
  201. if (!(is_required_class && "" === jobpost_form_inputs[i]['value'])) {
  202. if ("email" === input_type) {
  203. var error_element = $("span", element.parent());
  204. } else if ("phone" === input_type) {
  205. var error_element = $("#" + jobpost_form_inputs[i]['name'] + "-invalid-phone");
  206. }
  207. // Set Error Indicator on Invalid Input
  208. if (!valid) {
  209. error_element.show();
  210. error_free = false;
  211. }
  212. else {
  213. error_element.hide();
  214. }
  215. // Stop Form Submission
  216. if (!error_free) {
  217. event.preventDefault();
  218. }
  219. }
  220. }
  221. return error_free;
  222. }
  223. /**
  224. * Remove Required Attribute from Checkbox Group -> When one of the option is selected.
  225. *
  226. * Add Required Attribute from Checkboxes Group -> When none of the option is selected.
  227. *
  228. * @since 2.3.0
  229. */
  230. var requiredCheckboxes = $(':checkbox[required]');
  231. requiredCheckboxes.on('change', function () {
  232. var checkboxGroup = requiredCheckboxes.filter('[name="' + $(this).attr('name') + '"]');
  233. var isChecked = checkboxGroup.is(':checked');
  234. checkboxGroup.prop('required', !isChecked);
  235. });
  236. // Accept Numbers Input Only
  237. $(".sjb-numbers-only").keypress(function (evt) {
  238. evt = (evt) ? evt : window.event;
  239. var charCode = (evt.which) ? evt.which : evt.keyCode;
  240. if (charCode > 31 && (charCode < 48 || charCode > 57)) {
  241. return false;
  242. }
  243. return true;
  244. });
  245. });
  246. /*
  247. * Custom Styling of Upload Field Button
  248. *
  249. * @since 2.4.0
  250. */
  251. var file = {
  252. maxlength: 20, // maximum length of filename before it's trimmed
  253. convert: function () {
  254. // Convert all file type inputs.
  255. $('input[type=file].sjb-attachment').each(function () {
  256. $(this).wrap('<div class="file" />');
  257. $(this).parent().prepend('<div>'+ application_form.file['browse']+'</div>');
  258. $(this).parent().prepend('<span>'+ application_form.file['no_file_chosen']+'</span>');
  259. $(this).fadeTo(0, 0);
  260. $(this).attr('size', '50'); // Use this to adjust width for FireFox.
  261. });
  262. },
  263. update: function (x) {
  264. // Update the filename display.
  265. var filename = x.val().replace(/^.*\\/g, '');
  266. if (filename.length > $(this).maxlength) {
  267. trim_start = $(this).maxlength / 2 - 1;
  268. trim_end = trim_start + filename.length - $(this).maxlength + 1;
  269. filename = filename.substr(0, trim_start) + '&#8230;' + filename.substr(trim_end);
  270. }
  271. if (filename == '')
  272. filename = application_form.file['no_file_chosen'];
  273. x.siblings('span').html(filename);
  274. }
  275. }
  276. $(document).ready(function () {
  277. file.convert();
  278. $('input[type=file].sjb-attachment').change(function () {
  279. file.update($(this));
  280. });
  281. });
  282. })(jQuery);