Reputation: 245
I have this RV for a textbox in my page :
<asp:RangeValidator ID="titleRV" runat="server" ControlToValidate="titletxt" Font-Bold="True" Font-Size="Small" ForeColor="Red" MinimumValue="4" MaximumValue="100" Type="Integer" SetFocusOnError="True" Display="Dynamic" >Must be more than 3 !</asp:RangeValidator>
when I type any number of characters in my textbox at runtime the range validator appears whether I typed less than 3 letters or not ,and if I increased the number of letters it doesn't disappear! any help please ?
Upvotes: 1
Views: 3093
Reputation: 973
Please try this.....
<script type="text/javascript">
function test()
{
if (document.getElementById('titletxt').value.length < 3) {
document.getElementById('spMessage').style.display = '';
return false;
}
else {
document.getElementById('spMessage').style.display = 'none';
return true;
}
}
</script>
HTML....
<asp:TextBox runat="server" ID="titletxt" onblur="return test()"></asp:TextBox>
<span id='spMessage' style="font-weight:bold; font-size:small; color:Red; display:none">Enter more than 3 char!</span>
You can create this javascript and Call test() function to validate textbox....
Upvotes: 1
Reputation: 10191
A range validator is designed to check that you enter a value in a range - for example a number between 0 and 100. It sounds to me like you're trying to check how many letters were entered?
Assuming I've understood correctly my suggestion would be to use a RegEx validator.
You could use an expression like:
<asp:RegularExpressionValidator ID="valLength" runat="server"
ErrorMessage="Your text must be between 3 and 100 characters"
ValidationExpression="^[0-9a-zA-Z]{3,}$"
ControlToValidate="txtTextBox" />
This will obviously require tweaking depending on the characters you allow
Upvotes: 2
Reputation: 703
Rangevalidator is to check that the value entered falls between two values, not that it is a certain length. What you seem to be looking for are length validators
<asp:TextBox ID="txtTextBox" MaxLength="100" .../>
I don't think there is a minimum length validator built in, but you can easily add something in the code behind
if(txtTextBox.Length < 3)
// display error message
or a regular expression validator
<asp:RegularExpressionValidator ID="valLength" runat="server"
ErrorMessage="Must be between 3 and 100 characters"
ControlToValidate="txtTextBox"
ValidationExpression="^[a-zA-Z0-9'@&#.\s]{3,100}$" />
Upvotes: 2
Reputation: 27944
Your range validator validates if the number is between 4 and 100, not the number of chars in the textbox.
This will do the trick (CustomValidator):
<asp:TextBox runat="server" ID="titletxt" />
<asp:CustomValidator runat="server" ID="CustomValidator1" ControlToValidate="titletxt"
Text="The text length should be between 4 and 20"
ClientValidationFunction="clientValidate" Display="Dynamic">
</asp:CustomValidator>
<script type="text/javascript">
function clientValidate(sender, args) {
if (args.Value.length < 4 ||args.Value.length > 20) {
args.IsValid = false;
}
}
Upvotes: 1