/** * simple job board core front-end js file - v 1.4.0 * * @author presstigers , 2016 * * actions list * - job application submission callbacks * - date picker initialization * - validate email * - initialize telinput plugin * - validate phone number * - allowable uploaded file's extensions * - validate required inputs ( attachment, phone & email ) * - checkbox group required attribute callbacks * - custom styling of file upload button */ (function ($) { 'use strict'; $(document).ready(function () { var jobpost_submit_button = $('.app-submit'); $(".jobpost-form").on("submit", function (event) { $('.sjb-loading').show(); var jobpost_form_status = $('#jobpost_form_status'); var datastring = new formdata(document.getelementbyid("sjb-application-form")); /** * application form submit -> validate email & phone * * @since 2.2.0 */ var is_valid_email = sjb_is_valid_input(event, "email", "sjb-email-address"); var is_valid_phone = sjb_is_valid_input(event, "phone", "sjb-phone-number"); var is_attachment = sjb_is_attachment(event); /* stop form submission on invalid phone, email & file attachement */ if (!is_valid_email || !is_valid_phone || !is_attachment) { $('.sjb-loading').hide(); return false; } settimeout(function () { $.ajax({ url: application_form.ajaxurl, type: 'post', datatype: 'json', data: datastring, async: false, cache: false, contenttype: false, processdata: false, beforesend: function () { //$('.sjb-loading').show(); jobpost_submit_button.attr('disabled', 'diabled'); }, success: function (response) { if (response['success'] == true) { $('.jobpost-form').slideup(); /* translation ready string through script locaization */ jobpost_form_status.html(response['success_alert']); } if (response['success'] == false) { /* translation ready string through script locaization */ jobpost_form_status.html(response['error'] + ' ' + application_form.jquery_alerts['application_not_submitted'] + ''); $('.sjb-loading').hide(); jobpost_submit_button.removeattr('disabled'); } } }); }, 3000); return false; }); /* date picker */ $('.sjb-datepicker').datepicker({ dateformat: 'dd-mm-yy', changemonth: true, changeyear: true, yearrange: '-100:+50', }); /** * application form -> on input email validation * * @since 2.2.0 */ $('.sjb-email-address').on('input', function () { var input = $(this); var re = /^[a-za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-za-z0-9-]+(?:\.[a-za-z0-9-]+)*$/; var is_email = re.test(input.val()); var error_element = $(this).next(); if (is_email) { input.removeclass("invalid").addclass("valid"); error_element.hide(); } else { input.removeclass("valid").addclass("invalid"); } }); /** * initialize telinput plugin * * @since 2.2.0 */ if ( $('.sjb-phone-number').length ) { var telinput_id = $('.sjb-phone-number').map(function () { return this.id; }).get(); for (var input_id in telinput_id) { var telinput = $('#' + telinput_id[input_id]); telinput.intltelinput({ initialcountry: "auto", geoiplookup: function (callback) { $.get('https://ipinfo.io', function () { }, "jsonp").always(function (resp) { var countrycode = (resp && resp.country) ? resp.country : ""; callback(countrycode); }); }, }); } } /** * application form -> phone number validation * * @since 2.2.0 */ $('.sjb-phone-number').on('input', function () { var telinput = $(this); var telinput_id = $(this).attr('id'); var error_element = $("#" + telinput_id + "-invalid-phone"); error_element.hide(); // validate phone number if ($.trim(telinput.val())) { if (telinput.intltelinput("isvalidnumber")) { telinput.removeclass("invalid").addclass("valid"); error_element.hide(); } else { telinput.removeclass("valid").addclass("invalid"); } } }); /** * check for allowable extensions of uploaded file * * @since 2.3.0 */ $('.sjb-attachment').on('change', function () { var input = $(this); var file = $("#" + $(this).attr("id")); var error_element = file.parent().next("span"); error_element.text(''); error_element.hide(); // validate on file attachment if (0 != file.get(0).files.length) { /** * uploded file extensions checks * get uploded file ext */ var file_ext = file.val().split('.').pop().tolowercase(); // all allowed file extensions var allowed_file_exts = application_form.allowed_extensions; // settings file extensions && getting value from script localization var settings_file_exts = application_form.setting_extensions; var selected_file_exts = (('yes' === application_form.all_extensions_check) || null == settings_file_exts) ? allowed_file_exts : settings_file_exts; // file extension validation if ($.inarray(file_ext, selected_file_exts) > -1) { jobpost_submit_button.attr('disabled', false); input.removeclass("invalid").addclass("valid"); } else { /* translation ready string through script locaization */ error_element.text(application_form.jquery_alerts['invalid_extension']); error_element.show(); input.removeclass("valid").addclass("invalid"); } } }); /** * stop form submission -> on required attachments * * @since 2.3.0 */ function sjb_is_attachment(event) { var error_free = true; $(".sjb-attachment").each(function () { var element = $("#" + $(this).attr("id")); var valid = element.hasclass("valid"); var is_required_class = element.hasclass("sjb-not-required"); // set error indicator on invalid attachment if (!valid) { if (!(is_required_class && 0 === element.get(0).files.length)) { error_free = false; } } // stop form submission if (!error_free) { event.preventdefault(); } }); return error_free; } /** * stop form submission -> on invalid email/phone * * @since 2.2.0 */ function sjb_is_valid_input(event, input_type, input_class) { var jobpost_form_inputs = $("." + input_class).serializearray(); var error_free = true; for (var i in jobpost_form_inputs) { var element = $("#" + jobpost_form_inputs[i]['name']); var valid = element.hasclass("valid"); var is_required_class = element.hasclass("sjb-not-required"); if (!(is_required_class && "" === jobpost_form_inputs[i]['value'])) { if ("email" === input_type) { var error_element = $("span", element.parent()); } else if ("phone" === input_type) { var error_element = $("#" + jobpost_form_inputs[i]['name'] + "-invalid-phone"); } // set error indicator on invalid input if (!valid) { error_element.show(); error_free = false; } else { error_element.hide(); } // stop form submission if (!error_free) { event.preventdefault(); } } } return error_free; } /** * remove required attribute from checkbox group -> when one of the option is selected. * * add required attribute from checkboxes group -> when none of the option is selected. * * @since 2.3.0 */ var requiredcheckboxes = $(':checkbox[required]'); requiredcheckboxes.on('change', function () { var checkboxgroup = requiredcheckboxes.filter('[name="' + $(this).attr('name') + '"]'); var ischecked = checkboxgroup.is(':checked'); checkboxgroup.prop('required', !ischecked); }); // accept numbers input only $(".sjb-numbers-only").keypress(function (evt) { evt = (evt) ? evt : window.event; var charcode = (evt.which) ? evt.which : evt.keycode; if (charcode > 31 && (charcode < 48 || charcode > 57)) { return false; } return true; }); }); /* * custom styling of upload field button * * @since 2.4.0 */ var file = { maxlength: 20, // maximum length of filename before it's trimmed convert: function () { // convert all file type inputs. $('input[type=file].sjb-attachment').each(function () { $(this).wrap('
'); $(this).parent().prepend('
' + application_form.file['browse'] + '
'); $(this).parent().prepend('' + application_form.file['no_file_chosen'] + ''); $(this).fadeto(0, 0); $(this).attr('size', '50'); // use this to adjust width for firefox. }); }, update: function (x) { // update the filename display. var filename = x.val().replace(/^.*\\/g, ''); if (filename.length > $(this).maxlength) { trim_start = $(this).maxlength / 2 - 1; trim_end = trim_start + filename.length - $(this).maxlength + 1; filename = filename.substr(0, trim_start) + '…' + filename.substr(trim_end); } if (filename == '') filename = application_form.file['no_file_chosen']; x.siblings('span').html(filename); } } $(document).ready(function () { file.convert(); $('input[type=file].sjb-attachment').change(function () { file.update($(this)); }); }); })(jquery);