Reputation: 1112
Let me start by stating what I am really trying to do: I have to check a field for multiple "exclusive" (I'll explain) criteria, and display a single validation message.
Here is my code:
<asp:TextBox ID="Amount" runat="server" CssClass="field"></asp:TextBox>
<asp:RequiredFieldValidator Text="Amount is required"/>
<asp:CompareValidator Operator="DataTypeCheck" Type="Currency" Text="Amount must be numeric"/>
<asp:CompareValidator Operator="GreaterThanEqual" ValueToCompare="10" Type="Currency" Text="Minimum donation $10.00"/>
<asp:CompareValidator Operator="LessThan" ValueToCompare="10000" Type="Currency" Text="Wow that's too much"/>
I have left off some tags for readability - all validators are in the same validation group, they have ID's, ControlToValidate="Amount" Display="Dynamic"
.
If the Amount field is blank, or has a number in it, all is fine. However, if I put text in the field:
Even though the <10, >10,000 and DataTypeCheck should be exclusive, the numeric comparisons fail on the string.
I realize I could use a CustomValidator
for this, but I would like to eventually style one the messages differently, in its own separate <span>
element.
So, my incorrect (because they aren't "What am I really trying to do?") questions are:
CompareValidator
of currency type not fail on text? orUpvotes: 0
Views: 2289
Reputation: 1112
So I ended up using a CustomValidator
and setting a label where I wanted my different message.
.aspx page:
<span>
<asp:TextBox ID="Amount" runat="server" CssClass="field"></asp:TextBox>
<asp:RequiredFieldValidator [same as earlier...] />
<asp:CustomValidator ID="AmountCustomValidator" ControlToValidate="Amount" ValidateEmptyText="False" OnServerValidate="AmountCustomValidator_ServerValidate"/>
</span>
<span>
<asp:Label ID="AmountTooMuchMessage" runat="server" Text="" />
</span>
Code-behind:
protected void AmountCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = true;
AmountTooMuchMessage.Text = ""; //Reset special error label text
decimal amountFormatted;
if (!decimal.TryParse(args.Value, out amountFormatted))
{
AmountCustomValidator.Text = "Amount must be numeric";
args.IsValid = false;
return; //Don't try to compare values to non-numeric input
}
//This is money, so make it two decimals
amountFormatted = Math.Round(amountFormatted, 2);
Amount.Text = amountFormatted.ToString();
if (amountFormatted < 10)
{
AmountCustomValidator.Text = "Minimum donation $10.00";
args.IsValid = false;
}
if (amountFormatted > 10000)
{
AmountCustomValidator.Text = "";
AmountTooMuchMessage.Text = "That's too much";
args.IsValid = false;
}
}
Note that I had to remove the ErrorMessage
tag on the CustomValidator
, otherwise it will show when Text=""
.
Upvotes: 1
Reputation: 1013
$(document).ready(function() {
$("#txtboxToFilter").keydown(function(event) {
// Allow only backspace and delete
if ( event.keyCode == 46 || event.keyCode == 8 ) {
// let it happen, don't do anything
}
else {
// Ensure that it is a number and stop the keypress
if (event.keyCode > 48 || event.keyCode < 57 ) {
event.preventDefault();
}
}
});
});
Also, if you are using HTML5 you could simply use
EDIT 1 corrected the comparison operator to allow digits 0-9.
Upvotes: 1