ainoy31 Posted September 24, 2008 Share Posted September 24, 2008 Is it possible to validate hidden fields before submitting a form? I know some of you will ask why since it is a hidden field but just want to know. If so, please provide a link since I can not find one via google. Thank you. AM Quote Link to comment Share on other sites More sharing options...
efficacious Posted September 24, 2008 Share Posted September 24, 2008 yes you can.. using JS hidden field is just like any other input just hidden from users view.. If the input has a name or id you can check it just the same as any other. Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted September 24, 2008 Author Share Posted September 24, 2008 Yeah. I tried what you have stated and it does not seem to work. Here are the hidden fields. $skipCovCheckPage = "/customers/sales/productGroups.php?customer_id=12" <form name="dataForm" method="POST" action="<?php echo $skipCovCheckPage; ?>"> <input type="hidden" name="city" value="<?=$data['city'];?>"> <input type="hidden" name="state" value="<?=$data['state'];?>"> <input type="hidden" name="zip_code" value="<?=$data['zip_code'];?>"> <input type="hidden" name="phone_1" value="<?=$data['phone_1'];?>"> <input type="hidden" name="region_id" value="<?=$data['region_id'];?>"> <input type="hidden" name="saleType" value="<?=$data['saleType'];?>"> <div class="ButtonContainer"> <a class="Button Primary" href="#" id="FormSubmit" onClick="return validateService();"><span>Continue</span></a> </div> </form> The javascript: function validateService() { if(document.dataForm.elements['address_1'].value == "") { alert("Street Name is invalid."); return false; } if(document.dataForm.elements['city'].value == "") { alert("City is invalid."); return false; } if(document.dataForm.elements['state'].value == "") { alert("State is invalid."); return false; } if(document.dataForm.elements['zip_code'].value == "") { alert("Postal Code is invalid."); return false; } if(document.dataForm.elements['phone_1'].value == "") { alert("Phone 1 is invalid."); return false; } document.dataForm.action = '<?php echo $skipCovCheckPage; ?>'; document.dataForm.submit(); } The alert message will popup even though the street name field is populated. Much appreciation. AM Quote Link to comment Share on other sites More sharing options...
efficacious Posted September 24, 2008 Share Posted September 24, 2008 its working for me with this test code <script> function CheckForm() { if(document.getElementById('CM').value=='') { alert('THERE IS NO VALUE'); } else { alert("The Value of the input is "+ document.getElementById('CM').value); } } </script> <form action='' method='post'> <input type='hidden' value='CrackerMonkey' name='CM' id='CM' /> <input type='submit' value='submit' onmousedown='CheckForm();' /> </form> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.