Is there any way to use a field validator to check if one value is less than another?

M

Mufasa

I have two fields on a web page. The user enters the quantity they want.
Then there's another field that allows them to laminate the items (these are
posters). I want to make sure that the laminated count is less than or equal
to the quantity ordered. Is there a way to do that without doing an autopost
of the page? I'd like to use a validator that happens at the client end.

TIA - Jeff.
 
G

Guest

Jeff,

Please find an example below. Please also note it's easy to prepare a page
that skips your client validation therefore server validation should be
always present regardless of the client scripts:

<div>
<asp:TextBox runat="server" ID="quantityOrdered" Text="0" />
<asp:RequiredFieldValidator runat="server" ID="rfv1"
ErrorMessage="Please enter order quantity"
ControlToValidate="quantityOrdered" Display="Dynamic" />
<asp:RangeValidator runat="server" ID="rv1"
ErrorMessage="A valid number 0 ... 2000000"
ControlToValidate="quantityOrdered" Display="Dynamic"
MinimumValue="0" MaximumValue="2000000"
Type="Integer" />
</div>
<div>
<asp:TextBox runat="server" ID="laminatedCount" Text="0" />
<asp:RequiredFieldValidator runat="server" ID="rfv2"
ErrorMessage="Please enter number of items for lamination"
ControlToValidate="quantityOrdered" Display="Dynamic" />
<asp:RangeValidator runat="server" ID="rv2"
ErrorMessage="A valid number 0 ... 2000000"
ControlToValidate="laminatedCount" Display="Dynamic"
MinimumValue="0" MaximumValue="2000000"
Type="Integer" />
</div>
<div>
<asp:CustomValidator runat="server" ID="cv"
ControlToValidate="laminatedCount" Display="Dynamic"
ClientValidationFunction="ValidateQuantity"
OnServerValidate="cv_ServerValidate"
ErrorMessage="Number of laminated items must be less or equal items
ordered" />
</div>

<script type="text/javascript">
function ValidateQuantity(sender, e)
{
// ensure all the validators tested until now are valid
if (Page_IsValid)
{
var ordered =
parseInt(document.getElementById('<%=quantityOrdered.ClientID %>').value);
var laminated =
parseInt(document.getElementById('<%=laminatedCount.ClientID %>').value);

e.IsValid = laminated <= ordered;
}
}
</script>

<asp:Button runat="server" ID="submit" Text="Submit" />


HTH
 
M

Mufasa

Actually there's an even easier way - asp:CompareValidator

Works great and is all client side.

I'll still check on the server side though.

Thanks for the help.

J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top