Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.
$(document).ready(function() {
var form = $('#enquiry-form');
var requiredInput = $("#enquiry-form input[inputRequired], #enquiry-form textarea[inputRequired]");
var validEmail = new RegExp(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
// fields validation function
var validateRequired = function(evt) {
$(requiredInput).each(function() {
if (($.trim($(this).val() )=== "") || ($.trim($(this).val()).length <= 2)) {
$(this).addClass("error");
if ($(this).is('#first_name')) {
$(this).parent().find(".placeholder").text("Please enter your name");
} else if ($(this).is('#last_name')) {
$(this).parent().find(".placeholder").text("Please enter your last name");
} else if ($(this).is('#email')) {
$(this).parent().find(".placeholder").text("Please enter your email address");
}
evt.preventDefault();
} else {
$(this).removeClass("error");
$(this).parent().find(".placeholder").text("");
}
});
};
// end fields validation function
// email validation function
var validateEmail = function(evt) {
if($(this).val().match(validEmail)) {
$(this).removeClass("error");
} else {
$(this).addClass("error");
$(this).parent().find(".placeholder").text("Please enter a valid email address");
evt.preventDefault();
}
};
// end email validation function
$(requiredInput).blur(validateRequired);
$("#email").blur(validateEmail);
$(form).on("submit", function(evt) {
evt.preventDefault();
validateRequired.call($(requiredInput),evt);
if (validateRequired.call($(requiredInput),evt) && validateEmail.call($('email'),evt)) {
formError();
} else {
submitForm();
}
});
function submitForm() {
var formData = $(form).serialize();
var first_name = $("#first_name").val();
var last_name = $("#last_name").val();
var email = $("#email").val();
var oid = '0000';
var lead_source = 'Exclusive....';
$.ajax({
type: "POST",
url: "https://www.salesforce.com/......",
dataType: "json",
data: formData,
complete: function() {
$(form).css("opacity", 0.5);
$(".enquiry-confirmation").show();
}
});
} // end of submitForm function
function formError() {
// printing some error text
}
});
function prepForm() {
var form = $('#enquiry-form');
var formData = $(form).serialize();
var first_name = $("#first_name").val();
var last_name = $("#last_name").val();
var email = $("#email").val();
var oid = '0000';
var lead_source = 'Exclusive....';
var requiredInput = $("#enquiry-form input[inputRequired], #enquiry-form textarea[inputRequired]");
var validEmail = new RegExp(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
// fields validation function
var validateRequired = function(evt) {
$(requiredInput).each(function() {
if (($.trim($(this).val() )=== "") || ($.trim($(this).val()).length <= 2)) {
$(this).addClass("error");
if ($(this).is('#first_name')) {
$(this).parent().find(".placeholder").text("Please enter your name");
} else if ($(this).is('#last_name')) {
$(this).parent().find(".placeholder").text("Please enter your last name");
} else if ($(this).is('#email')) {
$(this).parent().find(".placeholder").text("Please enter your email address");
}
evt.preventDefault();
} else {
$(this).removeClass("error");
$(this).parent().find(".placeholder").text("");
}
});
};
// end fields validation function
// email validation function
var validateEmail = function(evt) {
if($(this).val().match(validEmail)) {
$(this).removeClass("error");
} else {
$(this).addClass("error");
$(this).parent().find(".placeholder").text("Please enter a valid email address");
evt.preventDefault();
}
};
// end email validation function
$(requiredInput).blur(validateRequired);
$("#email").blur(validateEmail);
$(form).on("submit", function(evt) {
evt.preventDefault();
validateRequired.call($(requiredInput),evt);
if (validateRequired.call($(requiredInput),evt) && validateEmail.call($('email'),evt)) {
formError();
} else {
submitForm( formData );
}
});
}
function submitForm( formData ) {
$.ajax({
type: "POST",
url: "https://www.salesforce.com/......",
dataType: "json",
data: formData,
complete: function() {
$(form).css("opacity", 0.5);
$(".enquiry-confirmation").show();
}
});
} // end of submitForm function
function formError() {
// printing some error text
}
$(document).ready(function() {
prepForm();
});
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
So your code will always hit the 'else' part of the condition.
Something like this:
Open in new window