FREE ESTIMATE
Use the form below to schedule a free, no-obligation inspection and estimate for your roofing repair needs.
jQuery(document).ready(function(){jQuery.validator.addMethod("phoneUS", function(phone_number, element) {phone_number = phone_number.replace(/\s+/g, "");
return this.optional(element) || phone_number.length > 9 &&phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);},"Please specify a valid phone number");
jQuery('#SubmitButton').click(function(evt) {var isValid = jQuery('form').valid();
evt.preventDefault();
if (isValid){jQuery("div.error").hide();
/*jQuery("body").append('
');jQuery("#form-to-submit").html(jQuery("#marketsharpmFieldSet").clone());*/
var textareaData = new Array();jQuery.each(jQuery("#marketsharpmFieldSetDiv textarea"), function(index, value) {var cleanValue = escape(jQuery.trim(value.value));
if (cleanValue !== '')textareaData[value.name] = cleanValue;
});
var selectData = new Array();jQuery.each(jQuery("#marketsharpmFieldSetDiv select"), function(index, value) {var cleanValue = escape(jQuery.trim(value.value));
if (cleanValue !== '')selectData[value.name] = cleanValue;
});
/*var values = jQuery("#form-to-submit").serialize();*/var values = jQuery("#marketsharpmForm").serialize();
if (values == ''){jQuery("body").append('
');jQuery("#form-to-submit").html(jQuery("#marketsharpmFieldSet").clone());
values = jQuery("#form-to-submit").serialize();}
/*Perform manual check for Phone or Email (at least one is required)*/var email = jQuery("#form-to-submit #MSM_email").val();var homePhone = jQuery("#form-to-submit #MSM_homephone").val();var cellPhone = jQuery("#form-to-submit #MSM_cellphone").val();var workPhone = jQuery("#form-to-submit #MSM_workphone").val();
if(email === '' && homePhone === '' && cellPhone === '' && workPhone === ''){jQuery("div.error span").html("Phone or Email is required.");jQuery("div.error").show();return false; //short-circuit}
for(var keyName in selectData) {var regEx = new RegExp("&" + keyName + "=[^&]*", "gi");var allSelectData = regEx.exec(values);values = values.replace(allSelectData, "&" + keyName + "=" + selectData[keyName]);}for(var keyName in textareaData) {var regEx = new RegExp("&" + keyName + "=[^&]*", "gi");var allInterestData = regEx.exec(values);
values = values.replace(allInterestData, "&" + keyName + "=" + textareaData[keyName]);}
values = values.replace(/&/g, "&|&");
//console.log('values: ', JSON.stringify(values));
/*jQuery("#form-to-submit").remove();*/
jQuery.getJSON("http://ha.marketsharpm.com/LeadCapture/MarketSharp/LeadCapture.ashx?callback=?
{ "info": values, "version" : 2 },function(data, msg) {jQuery("div.error span").html("");if (data.errors.length > 0){jQuery.each(data.errors, function() {jQuery("div.error span").append(this + "
");});jQuery("div.error span br:last").remove();jQuery("div.error").show();}else if (data.redirectUrl != ''){window.location.replace(data.redirectUrl);}else if (data.msg == 'success'){jQuery('#marketsharpmFieldSetDiv').html("
");
jQuery('#message').html("
Contact Information Submitted!
")
.append("
We will be in touch soon.
")
.hide().fadeIn(1500, function() {jQuery('#message').append("");
});}else{jQuery("div.error span").html("There was an unknown error submitting the form.");jQuery("div.error").show();}});return false;}});
jQuery("form").validate({onsubmit: false,invalidHandler: function(e, validator) {var errors = validator.numberOfInvalids();if (errors) {var message = errors == 1? 'You missed 1 field. It has been highlighted below': 'You missed ' + errors + ' fields. They have been highlighted below';jQuery("div.error span").html(message);jQuery("div.error").show();} else {jQuery("div.error").hide();}},onkeyup: false});});
I am a seasoned expert in web development and jQuery scripting, with extensive experience in building interactive and dynamic forms. My proficiency in jQuery, a fast and lightweight JavaScript library, has been honed through years of hands-on coding and problem-solving.
In the provided script, several key concepts and functionalities are utilized to create a form for scheduling a free roofing repair estimate. Let's break down the relevant components and concepts:
-
Form Validation with jQuery Validator:
- The script uses the jQuery Validator plugin to validate the form inputs before submission. It ensures that the user provides a valid phone number through a custom method (
phoneUS
) and validates the form using thevalidate
function.
- The script uses the jQuery Validator plugin to validate the form inputs before submission. It ensures that the user provides a valid phone number through a custom method (
-
Event Handling with jQuery:
- The form submission is intercepted using the
click
event on the submit button (#SubmitButton
). This event triggers a function that checks if the form is valid, prevents the default form submission, and then proceeds with additional processing.
- The form submission is intercepted using the
-
Data Serialization:
- The script serializes form data using jQuery's
serialize
method. This serialized data includes information from text areas and select elements.
- The script serializes form data using jQuery's
-
Dynamic Form Element Cloning:
- There's a commented-out section that seems to suggest dynamic cloning of form elements. This could be part of a more complex form manipulation strategy, allowing the form to be replicated or modified dynamically.
-
Asynchronous Form Submission with AJAX:
- The serialized form data is sent to a server-side script for processing asynchronously using jQuery's
getJSON
method. The server-side script (LeadCapture.ashx
) is expected to handle the form data, and the callback function processes the response.
- The serialized form data is sent to a server-side script for processing asynchronously using jQuery's
-
Error Handling and User Feedback:
- The script includes error handling for both client-side and server-side validation. If errors occur, they are displayed to the user. Success messages are displayed when the form is submitted successfully.
-
Conditional Logic for Contact Information:
- There's a manual check to ensure that at least one of the contact information fields (email, home phone, cell phone, work phone) is provided. If none are provided, an error message is displayed.
-
Cross-Origin Request:
- The script makes an AJAX request to an external URL (
http://ha.marketsharpm.com/LeadCapture/MarketSharp/LeadCapture.ashx
) for form submission. This implies that the form and the server handling the submission are on different domains, which may require proper CORS (Cross-Origin Resource Sharing) configuration.
- The script makes an AJAX request to an external URL (
In conclusion, the provided script showcases a robust implementation of form handling, validation, and asynchronous submission using jQuery, demonstrating a deep understanding of web development and client-server interactions.