January 13, 201511 yr Hi Gang, Now js is not my strong point but i'm trying to pretty up a form. I've hit a bug I cannot work out. The problem is that it sort of works. On submit with all blank but a file in the img_ext and img_int fields the form it returns false and highlights fine. If you complete just name, email or phone combination of two and files in the file fields it validates fine and prevents upload. The weird bit is if you complete all but the model field it proceeds to upload regardless that the model field is blank. Any ideas? The form ... <form name="valreq" action="" method="post" class="form" id="req" enctype="multipart/form-data"> <h2 class="yk hblock"><span class="name" itemprop="name">Your Details</span></h2> <div id="enqDets"></div> <fieldset> <ul class="table fs-1"> <li><label for="name">Name:</label><div><input name="name" id="name" type="text" class="input" value=""></div></li> <li><label for="email">Email:</label><div><input name="email" id="email" type="text" class="input" value=""></div></li> <li><label for="phone">Phone:</label><div><input name="phone" id="phone" type="text" class="input" value=""></div></li> </ul> </fieldset> <div class="clear8"></div> <h2 class="yk hblock"><span class="name" itemprop="name">Vehicle Details</span></h2> <div id="campDets"></div> <fieldset> <ul class="table fs-1"> <li><label for="model">Make & Model:</label><div><input name="model" id="model" type="text" class="input" value=""></div></li> </ul> </fieldset> <fieldset> <ul class="table fs-1"> <li><label for="img_ext">Exterior Image:</label><div><input name="imgs[]" id="img_ext" type="file" style="background: #fff;" class="input"></div></li> <li><label for="img_int">Interior Image:</label><div><input name="imgs[]" id="img_int" type="file" style="background: #fff;" class="input"></div></li> </ul> </fieldset> <div class="center liner"><input name="submit" type="submit" class="submit yk fs2" value="Send"></div> </form> The validation ... $(document.forms['valreq']).submit(sellvalidate); function sellvalidate() { $("body").find('#loader').show(); var enqForm = document.forms['valreq']; $('#enqDets').removeClass('errMsg').removeClass('liner').html(''); $('#campDets').removeClass('errMsg').removeClass('liner').html(''); $('#campSec').removeClass('errMsg').removeClass('liner').html(''); $(enqForm).find('input, label, select, div').removeClass('errbox'); if(enqForm.name.value=="") { $(enqForm.name).parent('div').addClass('errbox'); $(enqForm.name).parent('div').prev('label').addClass('errbox'); $(enqForm.name).focus(); $('#enqDets').addClass('errMsg').addClass('liner').html('Please provide your name.'); $("html, body").animate({ scrollTop: $('#enqDets').offset().top - 20 }, 1000); $("body").find('#loader').hide(); return false; }; if(enqForm.email.value=="") { $(enqForm.email).parent('div').addClass('errbox'); $(enqForm.email).parent('div').prev('label').addClass('errbox'); $(enqForm.email).focus(); $('#enqDets').addClass('errMsg').addClass('liner').html('Please provide your email.'); $("html, body").animate({ scrollTop: $('#enqDets').offset().top - 20 }, 1000); $("body").find('#loader').hide(); return false; } else { var emt = $(enqForm.email).val(); email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i; if(!email_regex.test(emt)){ $(enqForm.email).parent('div').addClass('errbox'); $(enqForm.email).parent('div').prev('label').addClass('errbox'); $(enqForm.email).focus(); $('#enqDets').addClass('errMsg').addClass('liner').html('Please provide a valid email address.'); $("html, body").animate({ scrollTop: $('#enqDets').offset().top - 20 }, 1000); $("body").find('#loader').hide(); return false; } }; if(enqForm.phone.value=="") { $(enqForm.phone).parent('div').addClass('errbox'); $(enqForm.phone).parent('div').prev('label').addClass('errbox'); $(enqForm.phone).focus(); $('#enqDets').addClass('errMsg').addClass('liner').html('Please provide your phone number.'); $("html, body").animate({ scrollTop: $('#enqDets').offset().top - 20 }, 1000); $("body").find('#loader').hide(); return false; } else { var phone = $(enqForm.phone).val(); intRegex = '/[0-9 -()+]+$/'; if((phone.length < 6) || (!intRegex.test(phone))) { $(enqForm.phone).parent('div').addClass('errbox'); $(enqForm.phone).parent('div').prev('label').addClass('errbox'); $(enqForm.phone).focus(); $('#enqDets').addClass('errMsg').addClass('liner').html('Please provide a valid phone number.'); $("html, body").animate({ scrollTop: $('#enqDets').offset().top - 20 }, 1000); $("body").find('#loader').hide(); return false; } }; if(enqForm.model.value=="") { $(enqForm.model).parent('div').addClass('errbox'); $(enqForm.model).parent('div').prev('label').addClass('errbox'); $(enqForm.model).focus(); $('#campDets').addClass('errMsg').addClass('liner').html('Please provide the vehicle make and model.'); $("html, body").animate({ scrollTop: $('#campDets').offset().top - 20 }, 1000); $("body").find('#loader').hide(); return false; }; }
January 13, 201511 yr Author intRegex = '/[0-9 -()+]+$/'; // should be intRegex = /[0-9 -()+]+$/; Found .... glad but freaking irritating.
Create an account or sign in to comment