Tuesday, 13 August 2013

.on('click' function not always firing - often takes 2 clicks

.on('click' function not always firing - often takes 2 clicks

I have a form validation script, which should fire upon clicking the
submit button. Everything works well, except sometimes it requires the
submit button to be clicked twice before going. The fact is only happens
about 20% of the time makes it really hard to diagnose. The #send button
is not dynamic, so I'm not sure what the issue could be. Any help would be
appreciated! This will be used on a mobile form, so any ideas for any
other improvements as well would be great. Thanks!
$(document).ready(function(){
var jVal = {
'location' : function() {
var element = $('#location');
var errorMsg = $('#LocationError');
if(element.val()==0) {
jVal.errors = true;
errorMsg.slideDown('fast');
element.addClass('wrong');
} else {
errorMsg.slideUp('fast');
element.removeClass('wrong');
}
},
'firstName' : function() {
var element = $('#firstname');
var errorMsg = $('#nameInfo');
if(element.val().length < 1) {
jVal.errors = true;
errorMsg.slideDown('fast');
element.addClass('wrong');
} else {
errorMsg.slideUp('fast');
element.removeClass('wrong');
}
},
'lastName' : function() {
var element = $('#lastname');
var errorMsg = $('#LastNameInfo');
if(element.val().length < 1) {
jVal.errors = true;
errorMsg.slideDown('fast');
element.addClass('wrong');
} else {
errorMsg.slideUp('fast');
element.removeClass('wrong');
}
},
'email' : function() {
var element = $('#email');
var errorMsg = $('#EmailError');
var patt =
/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if(element.val().length < 1) {
jVal.errors = true;
errorMsg.html('Email is required.').slideDown('fast');
element.addClass('wrong');
} else if (!patt.test(element.val())) {
jVal.errors = true;
errorMsg.html('Please enter a valid email
address.').slideDown('fast');
element.addClass('wrong');
} else {
errorMsg.slideUp('fast');
element.removeClass('wrong');
}
},
'telephone' : function() {
var element = $('#telephone');
var errorMsg = $('#TelephoneInfo');
var patt = /^(?:\+?1[-. ]?)?(\(\d{3}\)|\d{3})-?\d{3}-?\d{4}$/;
if(element.val().length < 1) {
jVal.errors = true;
errorMsg.html('Telephone is required.').slideDown('fast');
element.addClass('wrong');
} else if (!patt.test(element.val())) {
jVal.errors = true;
errorMsg.html('Please enter a valid telephone
number.').slideDown('fast');
element.addClass('wrong');
} else {
errorMsg.slideUp('fast');
element.removeClass('wrong');
}
},
'sendIt' : function (){
var element = $('#send'),
submissionInfo = '<div id="SubmissionInfo" class=""></div>',
successMSG = '<strong>Thank you for contacting
us!</strong><br>An Ideal Image consultant will contact you
soon!</i>',
sending = '<i class="icon-spinner icon-spin icon-2x"
style="color:#5383b4;"></i>',
errors = $('.error');
if($("#SubmissionInfo").length==0) {
$(element).before(submissionInfo);
}
submissionInfo = $('#SubmissionInfo');
if(!jVal.errors) {
//form.submit();
var form = $('#myform'),
trackingURL = window.location.pathname+'thankyou.php';
submissionInfo.html(sending).fadeIn('fast');
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data: form.serialize(),
success: function(){
form.get(0).reset();
submissionInfo.html(successMSG).addClass('success');
_gaq.push(['_trackPageview',trackingURL]);
jVal.firePixel();
}
});
} else {
errors.animate({marginLeft:'+='+5},150,function(){$(this).animate({marginLeft:'-='+5},150);});
}
},
'firePixel' : function() {
//Chango
var __chconv__ = {"conversion_id":14};
(function() {
if (typeof(__chconv__) == "undefined") return;
var e = encodeURIComponent; var p = [];
for(var i in __chconv__){p.push(e(i) + "=" + e(__chconv__[i]))}
(new Image()).src = document.location.protocol +
'//as.chango.com/conv/i;' + (new Date()).getTime() + '?' +
p.join("&");
})();
//VWO
if(typeof(_vis_opt_top_initialize) == "function") {
// Code for Custom Goal: Goal #2
_vis_opt_goal_conversion(200);
// uncomment the following line to introduce a half second pause
to ensure goal always registers with the server
_vis_opt_pause(500);
}
}
};
// ====================================================== //
// Validation
$('#send').on('click', function (){
jVal.errors = false;
jVal.location();
jVal.firstName();
jVal.lastName();
jVal.telephone();
jVal.email();
jVal.sendIt();
return false;
});
$('#location').change(jVal.location);
$('#firstname').change(jVal.firstName);
$('#lastname').change(jVal.lastName);
$('#email').change(jVal.email);
$('#telephone').change(jVal.telephone);
});

No comments:

Post a Comment