/** | FORM VALIDATION | | Validate form inputs during input as well as on form submit | *** Do not continuously validate for mobile. Slows down big time *** | | Drop this into the ordeing page and change the target in $('blah').validate(...) | */ // Validate CC security codes $.validator.addMethod("CCExp", function(value, element, params) { var minMonth = new Date().getMonth() + 1; var minYear = new Date().getFullYear(); var month = parseInt($(params.month).val(), 10); var year = parseInt($(params.year).val(), 10); // Test if the selected year is gte the minimum year required if (year < minYear) { return false; } else if (year > minYear) { return true; } else if (year == minYear && month >= minMonth) { return true; } return false; }); $.validator.addMethod("IasExp", function(value, element, params) { var minMonth = new Date().getMonth() + 1; var minYear = new Date().getFullYear(); var month = parseInt($(params.month).val(), 10); var year = parseInt($(params.year).val(), 10); if ($('#IasNumber').val().length > 0) { // Test if the selected year is gte the minimum year required if (year < minYear) { return false; } else if (year > minYear) { return true; } else if (year == minYear && month >= minMonth) { return true; } return false; } return true; }); // Validate US Zip and CA Postal codes $.validator.addMethod("isValidZip", function(postal_code) { var country_code = $("#ShippingCountryCode").val(); switch (country_code) { case "US": postalCodeRegex = /^[0-9]{5}(?:[-\s]*[0-9]{4})?$/; break; case "CA": postalCodeRegex = /^[A-Z]\d[A-Z][ -]?\d[A-Z]\d$/i; break; default: postalCodeRegex = /.*/; break; } return postalCodeRegex.test(postal_code); }); // Validate as not being equal to 000 $.validator.addMethod("not000", function(value, element) { return (value !== '000') ? true : false; }); // VALIDATE SIGNUP FORM ON KEYUP AND SUBMIT $.validator.setDefaults({ debug: false, errorClass: "invalid", validClass: "valid", onkeyup: false, focusCleanup: true, focusInvalid: false, }); /** * ORDER FORM */ $("form#order_form").validate({ errorPlacement: function(error, element) { if (element.attr("name") == "AgreeTerms" ) { error.insertAfter("#opt_in_wrapper .js-error"); } else { error.insertAfter(element); } }, ignoreTitle: true, rules: { // BILLING INFORMATION ShippingFirstName: "required", ShippingLastName: "required", Email: { required: true, email: true }, ShippingPhone: "required", ShippingAddress: "required", ShippingCountryCode: { required: true, maxlength: 2 }, ShippingCity: "required", ShippingStateProvince: { required: true, not000: true }, ShippingPostalCode: { required: { // Checks if country requires a zip/postal code depends: function(element) { var country_code = $("#ShippingCountryCode").val(); var require_zip = ["US","CA","AU","GB"]; return ($.inArray(country_code, require_zip) !== -1) ? true : false; } }, isValidZip: { // Validate zip codes for US and CA entries depends: function(element) { var country_code = $("#ShippingCountryCode").val(); var check_zip = ["US"]; return ($.inArray(country_code, check_zip) !== -1) ? true : false; } } }, // BILLING INFORMATION BillingFirstName: { required: { depends: function(element) { return ($('#BillingAddress').val().length > 0) ? true : false; } } }, BillingLastName: { required: { depends: function(element) { return ($('#BillingAddress').val().length > 0) ? true : false; } } }, BillingPhone: { required: { depends: function(element) { return ($('#BillingAddress').val().length > 0) ? true : false; } } }, BillingAddress: { required: "billingFirstNameIsFilled"}, BillingCountryCode: { required: { depends: function(element) { return ($('#BillingAddress').val().length > 0) ? true : false; } } }, BillingCity: { required: { depends: function(element) { return ($('#BillingAddress').val().length > 0) ? true : false; } } }, BillingStateProvince: { required: { depends: function(element) { return ($('#BillingAddress').val().length > 0) ? true : false; } }, not000: { depends: function(element) { return ($('#BillingAddress').val().length > 0) ? true : false; } } }, BillingPostalCode: { required: { depends: function(element) { if ($('#BillingAddress').val().length > 0) { var country_code = $("#BillingCountryCode").val(); var require_zip = new Array("US","CA","AU","GB"); console.log('requiring shipping ZIP as add_billing_address came back checked'); return ($.inArray(country_code, require_zip) !== -1) ? true : false; } return false; } }, isValidZip: { depends: function(element) { if ($('#BillingAddress').val().length > 0) { var country_code = $("#BillingCountryCode").val(); var check_zip = new Array("US","CA","GB"); return ($.inArray(country_code, check_zip) !== -1) ? true : false; } return false; } } }, // SHIPPING OPTIONS ShippingOption: { required: true }, IasNumber: { required: { // IAS Number required if promo code is entered depends: function(element) { var promo_code = $("[name=promo-code]").val(); return promo_code.length > 0; } }, minlength: 16, maxlength: 19 }, IasExpYear: { IasExp: { month: "#IasExpMonth", year: "#IasExpYear" } }, // CARD INFORMATION CardNumber: { required: true, creditcard: true }, NameOnCard: { required: true, minlength: 3, maxlength: 120, }, CVC: { required: true, digits: true, rangelength: [3,4] }, ExpYear: { CCExp: { month: "#ExpMonth", year: "#ExpYear" } }, // Consents AgreeTerms: { required: true } }, messages: { // Shipping fields ShippingFirstName: 'De voornaam is vereist', ShippingLastName: 'De achternaam is vereist', Email: 'Vul alstublieft het e-mailadres in waar uw ontvangstbewijs naartoe moet worden gestuurd', ShippingPhone: 'Vul alstublieft een telefoonnummer in, we hebben dit nodig in verband met de verzending', ShippingAddress: 'Vul alstublieft het verzendadres in', ShippingCountryCode: 'Vul alstublieft het verzendadres in', ShippingCity: 'Vul alstublieft een stad in', ShippingStateProvince: 'Selecteer alstublieft een optie uit deze lijst', ShippingPostalCode: { required: 'Vul alstublieft een postcode in', isValidZip: 'Controleer of uw postcode correct is' }, // Billing field BillingFirstName: 'Vul alstublieft uw voornaam in', BillingLastName: 'Vul alstublieft uw achternaam in', BillingAddress: 'Vul alstublieft uw factuuradres in', BillingPhone: 'Vul alstublieft uw telefoonnummer in. Dit wordt alleen gebruikt voor vragen omtrent facturering en verzending', BillingCountryCode: 'Vul alstublieft het land voor de factuur in', BillingStateProvince: 'Selecteer alstublieft de rekening staat/provincie voor de factuur', BillingCity: 'Vul alstublieft de stad voor de factuur in', BillingPostalCode: { required: 'Vul alstublieft een postcode in', isValidZip: 'Controleer of uw postcode correct is' }, // Billing options ShippingOption: 'gcui_scnstore:shippingOption', // IAS Number IasNumber: { required: 'IAS Number is required to get a discount', minlength: 'The IAS Membership number should 16 digits long', maxlength: 'The IAS Membership number should 16 digits long' }, IasExpYear: 'Your membership has expired.', // Credit card fields CardNumber: { required: 'Uw kaartnummer is vereist', creditcard: 'Uw creditcardnummer lijkt incorrect, controleer het alstublieft opnieuw' }, ExpYear: 'Uw kaart is verlopen. Controleer alstublieft de datum', NameOnCard: 'Vermeld alstublieft de naam zoals vermeld op de kaart', CVC: { required: 'De veiligheidscode is vereist', digits: 'Alleen nummers', rangelength: '3-4 getallen' } }, invalidHandler: function(event, validator) { var errors = validator.numberOfInvalids(); if (errors) { $.each(validator.errorList, function(i, error) { if (error.element.id.match(/Billing/i)) { $('#address_card').addClass('flipped'); $('#billing_data').addClass('unflipped'); $('#add_billing').addClass('activated'); $('#add_billing .text').text('Keer terug naar Verzendadres'); } }); $.each(validator.errorList, function(i, error) { if (error.element.id.match(/Shipping/i)) { $('#address_card').removeClass('flipped'); $('#billing_data').removeClass('unflipped'); $('#add_billing').removeClass('activated'); $('#add_billing .text').text('Andere factuuradres?'); } }); var message = (errors == 1) ? 'U heeft een veld overgeslagen Het is in kleur aangegeven' : 'U heeft ' + errors + ' velden overgeslagen. Ze zijn in kleur aangegeven'; if (typeof window.ScnStore === "undefined") { $("div.jserror span").html(message); $("div.jserror").show(); $('html, body').animate({ scrollTop: $(validator.errorList[0].element).offset().top - 10 }, 800, 'linear', function() { $(validator.errorList[0].element).focus(); }); } else { window.ScnStore._buildErrorMessage('jsvalidate_msg', message); } // So there were no errors, nuke any messages on the screen } else { $("div.jserror").hide(); $("#jsvalidate_msg button").click(); } }, submitHandler: function(form) { var method = $('#checkout_btn').data('payment-method'); $('#checkout_btn').prop('disabled', true).addClass('wait') .html('Verwerken...'); if($('#checkout_btn').data('review') == true) { window.location.pathname = '/store/review.action'; return; } window.ScnStore.Checkout(method); } }); /** * DOWNLOAD FORM */ $("form#dn_download_form").validate({ errorPlacement: function(error, element) { if (element.attr("name") == "AgreeTerms" ) { error.insertAfter("#opt_in_wrapper .js-error"); } else { error.insertAfter(element); } }, ignoreTitle: true, rules: { firstName: "required", lastName: "required", email: { required: true, email: true }, // Consents agreeTerms: "required" }, messages: { firstName: 'De voornaam is vereist', lastName: 'De achternaam is vereist', email: 'Vul alstublieft het e-mailadres in waar uw ontvangstbewijs naartoe moet worden gestuurd' }, invalidHandler: function(event, validator) { var errors = validator.numberOfInvalids(); if (errors) { var message = (errors == 1) ? 'U heeft een veld overgeslagen Het is in kleur aangegeven' : 'U heeft ' + errors + ' velden overgeslagen. Ze zijn in kleur aangegeven'; if (typeof window.FormControl === "undefined") { $("div.jserror span").html(message); $("div.jserror").show(); $('html, body').animate({ scrollTop: $(validator.errorList[0].element).offset().top - 10 }, 800, 'linear', function() { $(validator.errorList[0].element).focus(); }); } else { window.FormControl._buildErrorMessage('jsvalidate_msg', message); } // So there were no errors, nuke any messages on the screen } else { $("div.jserror").hide(); $("#jsvalidate_msg button").click(); } }, submitHandler: function() { $("div.jserror").hide(); $("#jsvalidate_msg button").click(); // Put the form in to "processing" state $('#dn_download_btn').prop('disabled', true). html('Verwerken...'); // Do the form submission through FormControl window.FormControl.SubmitToReaches(); } }); /** * CONTACT FORM */ $("form#contact_form").validate({ errorPlacement: function(error, element) { if (element.attr("name") == "AgreeTerms" ) { error.insertAfter("#opt_in_wrapper .js-error"); } else { error.insertAfter(element); } }, ignoreTitle: true, rules: { firstName: "required", lastName: "required", email: { required: true, email: true }, body: { required: true, maxlength: 3000 }, // Consents agreeTerms: "required" }, messages: { firstName: 'De voornaam is vereist', lastName: 'De achternaam is vereist', email: 'Vul alstublieft het e-mailadres in waar uw ontvangstbewijs naartoe moet worden gestuurd', mobilePhone: 'Vul alstublieft een telefoonnummer in, we hebben dit nodig in verband met de verzending' }, invalidHandler: function(event, validator) { var errors = validator.numberOfInvalids(); if (errors) { var message = (errors == 1) ? 'U heeft een veld overgeslagen Het is in kleur aangegeven' : 'U heeft ' + errors + ' velden overgeslagen. Ze zijn in kleur aangegeven'; if (typeof window.FormControl === "undefined") { $("div.jserror span").html(message); $("div.jserror").show(); $('html, body').animate({ scrollTop: $(validator.errorList[0].element).offset().top - 10 }, 800, 'linear', function() { $(validator.errorList[0].element).focus(); }); } else { window.FormControl._buildErrorMessage('jsvalidate_msg', message); } // So there were no errors, nuke any messages on the screen } else { $("div.jserror").hide(); $("#jsvalidate_msg button").click(); } }, submitHandler: function() { $("div.jserror").hide(); $("#jsvalidate_msg button").click(); // Put the form in to "processing" state $('#'+this.currentForm.id + ' button').prop('disabled', true). html('Verwerken...'); // Do the form submission through FormControl window.FormControl.SubmitToReaches(); } }); /** * HARDCOPY REQUEST */ $("form.hardcopy-request-form").validate({ errorPlacement: function(error, element) { if (element.attr("name") == "AgreeTerms" ) { error.insertAfter("#opt_in_wrapper .js-error"); } else { error.insertAfter(element); } }, ignoreTitle: true, rules: { // BILLING INFORMATION firstName: "required", lastName: "required", email: { required: true, email: true }, address: "required", countryCode: { required: true, maxlength: 2 }, city: "required", statePorvince: { required: true, not000: true }, postalCode: { required: { // Checks if country requires a zip/postal code depends: function(element) { var country_code = $("#ShippingCountryCode").val(); var require_zip = ["US","CA","AU","GB"]; return ($.inArray(country_code, require_zip) !== -1) ? true : false; } }, isValidZip: { // Validate zip codes for US and CA entries depends: function(element) { var country_code = $("#ShippingCountryCode").val(); var check_zip = ["US"]; return ($.inArray(country_code, check_zip) !== -1) ? true : false; } } }, // Consents agreeTerms: { required: true } }, messages: { // Shipping fields firstName: 'De voornaam is vereist', lastName: 'De achternaam is vereist', email: 'Vul alstublieft het e-mailadres in waar uw ontvangstbewijs naartoe moet worden gestuurd', mobilePhone: 'Vul alstublieft een telefoonnummer in, we hebben dit nodig in verband met de verzending', address: 'Vul alstublieft het verzendadres in', address2: 'Vul alstublieft het verzendadres in', city: 'Vul alstublieft een stad in', stateProvince: 'Selecteer alstublieft een optie uit deze lijst', postalCode: { required: 'Vul alstublieft een postcode in', isValidZip: 'Controleer of uw postcode correct is' } }, invalidHandler: function(event, validator) { var errors = validator.numberOfInvalids(); if (errors) { var message = (errors == 1) ? 'U heeft een veld overgeslagen Het is in kleur aangegeven' : 'U heeft ' + errors + ' velden overgeslagen. Ze zijn in kleur aangegeven'; if (typeof window.FormControl === "undefined") { $("div.jserror span").html(message); $("div.jserror").show(); $('html, body').animate({ scrollTop: $(validator.errorList[0].element).offset().top - 10 }, 800, 'linear', function() { $(validator.errorList[0].element).focus(); }); } else { window.FormControl._buildErrorMessage('jsvalidate_msg', message); } } // So there were no errors, nuke any messages on the screen else { $("div.jserror").hide(); $("#jsvalidate_msg button").click(); } }, submitHandler: function() { $("div.jserror").hide(); $("#jsvalidate_msg button").click(); // Put the form in to "processing" state $('#'+this.currentForm.id + ' button').prop('disabled', true). html('Verwerken...'); // Do the form submission through FormControl window.FormControl.SubmitToReaches(); } });