Jump to content

Scripting Question - Field Entry Validation


Recommended Posts

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">

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...

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">
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...