csporcic Posted August 20, 2013 Report Share Posted August 20, 2013 Here is another scripting question related to validating a field entry. The scenario is that users order products. There is a MaxQty field in the product table that identifies the maximum quantity that can be ordered of each product. The current script is listed below, which is broker into 2 parts. 1) Function "Drop" takes the value from a cascading dropdown field (Virtual2), which is the maximum order quantity for the item selected, and populates a text field called MaxQ with the same value. This function works correctly. 2) Function "ValidateNumbers" should compare the value in field "Qty" entered by the user with the value in field "MaxQ" determined by the selected product. It should display an error message when Qty is greater than MaxQ and prevent the form from submitting. This function is only allowing the form to submit when the Qty is equal to the MaxQ. It should allow the form to submit when the Qty is less than MaxQ, but it does not. Please review the code and let me know if you have any suggestions. Thanks, Chris. <script type="text/javascript"> function drop(){ document.getElementById('InsertRecordMaxQ').value = document.getElementsByName('cbParamVirtual2')[0].value; } document.getElementsByName('cbParamVirtual2')[0].onchange = drop; function ValidateNumbers(){ if (document.getElementById('InsertRecordQty').value >document.getElementById("InsertRecordMaxQ").value ) { alert("The pieces ordered cannot exceed the maximum quantity."); document.getElementById('InsertRecordQty').value = null; return false; } else {document.getElementById('caspioform').submit();} } </script> <input type="image" src="images/Step1.gif" height="22" width="280" onclick="ValidateNumbers()" value="Submit"> Quote Link to comment Share on other sites More sharing options...
MayMusic Posted August 21, 2013 Report Share Posted August 21, 2013 Is it possible for you to give me the URL of the page? Quote Link to comment Share on other sites More sharing options...
csporcic Posted August 21, 2013 Author Report Share Posted August 21, 2013 Here is a link to the deployed datapage. I added some comments after a few of the fields. http://bridge.caspio.net/dp.asp?AppKey=d39010003c6f200f7e554a98b8cb The script works some of the time, but not all of the time. The alert message should only appear if the Qty is greater than the MaxQ, otherwise the form should submit. Thanks for looking into this, Chris. Quote Link to comment Share on other sites More sharing options...
csporcic Posted September 2, 2013 Author Report Share Posted September 2, 2013 I figured it out, here is the script. Chris. <script type="text/javascript"> function drop(){ document.getElementById('InsertRecordMaxQ').value = document.getElementsByName('cbParamVirtual2')[0].value; } document.getElementsByName('cbParamVirtual2')[0].onchange = drop; function ValidateNumbers(){ var v_maxqty = parseInt(document.getElementById("InsertRecordMaxQ").value); var v_pieces = parseInt(document.getElementById('InsertRecordQty').value); if(v_pieces > v_maxqty || v_pieces=="" || isNaN(v_pieces)) { alert("The pieces ordered cannot exceed the maximum quantity."); document.getElementById('InsertRecordQty').value = null; return false; } else {document.getElementById('caspioform').submit();} } </script> <input type="image" src="images/Step1.gif" height="22" width="280" onclick="ValidateNumbers()" value="Submit"> Quote Link to comment Share on other sites More sharing options...
bahar_vm Posted September 4, 2013 Report Share Posted September 4, 2013 Great! Thanks for sharing the script. 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.
Note: Your post will require moderator approval before it will be visible.