How do I limit the length for a string using RangeValidator instead of RegularExpressionValidator?
Say, I can simply set a RegularExpressionValidator's ValidationExpression to "(\s|.){0,5}" if i wanted to make sure an input has at most 5 characters.
But how do I do this with RangeValidator?
I tried setting RangeValidator's min value to 1 space, and max value to "~~~~~" because ascii value of a space is 32, and ascii value of "~" is 127. I was hoping the validator checks the input lexigraphically, but it did not work.
I think RangeValidator is not a good validator to validate the length of the string.You'd better use RegularExpressionValidator to validate it.
Range validator control is a validator control which checks to see if a control value is within a valid range. The attributes that are necessary to this control are:
MaximumValue, MinimumValue, and Type.
Here are some sample codes:
Enter a date from 1998:
<asp:textbox id="textbox1"runat="server"/>
<asp:RangeValidatorid="valRange"runat="server"ControlToValidate="textbox1"MaximumValue="12/31/1998"MinimumValue="1/1/1998"Type="Date"ErrorMessage="* The date must be between 1/1/1998 and 12/13/1998" Display="static">*</asp:RangeValidator>
Jasson, you are right, a regularexpressionvalidator is better to validator the length of an input.
My question is more about how does a RangeValidator compare string types.
If I put Minimium value as capital A and maximium value as small z, then its equal to [a-zA-Z] in regular expression.
But according to some source, it says range validators compare strings by ascii value, if that were true, characters 0-9 lie between A and z, so an equivalent regular expression would be [0-9a-zA-Z], which is not the case... that's why I am so confused and
hope you guys have some expertise.
msdn's doc on RangeValidator class isn't precise enough, at least not the part I looked at.
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
RangeValidator1.MinimumValue = "0";
RangeValidator1.MaximumValue = "z";
RangeValidator1.ControlToValidate = txt1.ID;
string s = "";
for (int i = 0; i < 128; i++)
{
txt1.Text = ""+ (char)i ;
RangeValidator1.Validate();
if (RangeValidator1.IsValid)
{
s += (char)i + " (" + i + ") is valid<br>";
}
}
// ASCII( '0' ) = 48
// ASCII( 'z' ) = 122
Guess what the output is?
(9) is valid (10) is valid (11) is valid (12) is valid (13) is
valid (32) is valid 0 (48) is valid ... '1' to '8' are removed for readability 9 (57) is valid A (65) is
valid ... 'B' to 'X' are valid and removed for readability Y (89) is
valid
[ where did 'Z' go ?? ] [ and where did the puncutations such as '[', '\', and ']' go? ]
a (97) is valid ... 'b' to 'y' are valid are removed for readability z (122) is valid
See? it does compare by ascii value, but not totally. I can't find a fitting explaination
A slightly different version of the test, by changing to
I give you a detailed explanation about asp:RangeValidator control for your reference.
The RangeValidator control is used to check that the user enters an input value that falls between two values. It is possible to check ranges within numbers, dates, and characters.
Note:
1.The validation will not fail if the input control is empty. Use the RequiredFieldValidator control to make the field required.
2.The validation will not fail if the input value cannot be converted to the data type specified. Use the CompareValidator control, with its Operator property set to ValidationCompareOperator.DataTypeCheck, to verify the data type of the input value.
3.Specifies the data type of the value to check. The types are:
Currency
Date
Double
Integer
String
4.The RangeValidator control throws an exception if the value specified by the
MaximumValue or MinimumValue property cannot be converted to the data type specified by the
Type property.
Marked as answer by Jasson_King on Nov 20, 2006 12:25 AM
The overview about RangeValidator is legitimate, but my question really is technically how it compares the input string against minimiumvalue and maximum value to make sure it's valid.
Here is the exact function used by the client-side validation code to compare two values:
function ValidatorCompare(operand1, operand2, operator, val) {
var dataType = val.type;
var op1, op2;
if ((op1 = ValidatorConvert(operand1, dataType, val)) == null)
return false;
if (operator == "DataTypeCheck")
return true;
if ((op2 = ValidatorConvert(operand2, dataType, val)) == null)
return true;
switch (operator) {
case "NotEqual":
return (op1 != op2);
case "GreaterThan":
return (op1 > op2);
case "GreaterThanEqual":
return (op1 >= op2);
case "LessThan":
return (op1 < op2);
case "LessThanEqual":
return (op1 <= op2);
default:
return (op1 == op2);
}
}
Here is the evaluation function for the RangeValidator.
function RangeValidatorEvaluateIsValid(val) {
var value = ValidatorGetValue(val.controltovalidate);
if (ValidatorTrim(value).length == 0)
return true;
return (ValidatorCompare(value, val.minimumvalue, "GreaterThanEqual", val) &&
ValidatorCompare(value, val.maximumvalue, "LessThanEqual", val));
}
You can see its using the > = < operators in javascript to compare strings. Those work based on ASCII comparisons.
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
PLBlum, I agree with you.It does ASCII comparision in the asp:RangeValidator control.
As we have known,the ASCII values of '0' to '9' are 48 to 57, the ASCII values of 'a' to 'z' are 97 to 122 , and the ASCII values of 'A' to 'Z' are 65 to 90.
If we set '0' as minValue and 'z' as maxValue of a asp:RangeValidator control,"0~9","a~z" and "A~Z" should pass validation in the range validator.
--- Peter Blum
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
Marked as answer by Jasson_King on Nov 27, 2006 08:44 AM
haoest
Participant
1768 Points
404 Posts
how to use RangeValidator for String type?
Nov 16, 2006 03:39 PM|LINK
How do I limit the length for a string using RangeValidator instead of RegularExpressionValidator?
Say, I can simply set a RegularExpressionValidator's ValidationExpression to "(\s|.){0,5}" if i wanted to make sure an input has at most 5 characters.
But how do I do this with RangeValidator?
I tried setting RangeValidator's min value to 1 space, and max value to "~~~~~" because ascii value of a space is 32, and ascii value of "~" is 127. I was hoping the validator checks the input lexigraphically, but it did not work.
Any pointers?
Jasson_King
Star
8057 Points
1520 Posts
Re: how to use RangeValidator for String type?
Nov 17, 2006 02:58 AM|LINK
I think RangeValidator is not a good validator to validate the length of the string.You'd better use RegularExpressionValidator to validate it.
Range validator control is a validator control which checks to see if a control value is within a valid range. The attributes that are necessary to this control are: MaximumValue, MinimumValue, and Type.
Here are some sample codes:
Wish this can give you some helps.>
haoest
Participant
1768 Points
404 Posts
Re: how to use RangeValidator for String type?
Nov 18, 2006 03:13 AM|LINK
Jasson, you are right, a regularexpressionvalidator is better to validator the length of an input.
My question is more about how does a RangeValidator compare string types.
If I put Minimium value as capital A and maximium value as small z, then its equal to [a-zA-Z] in regular expression.
But according to some source, it says range validators compare strings by ascii value, if that were true, characters 0-9 lie between A and z, so an equivalent regular expression would be [0-9a-zA-Z], which is not the case... that's why I am so confused and hope you guys have some expertise.
msdn's doc on RangeValidator class isn't precise enough, at least not the part I looked at.
PLBlum
All-Star
30409 Points
5347 Posts
MVP
Re: how to use RangeValidator for String type?
Nov 18, 2006 05:00 PM|LINK
It does compare by ASCII values.
Your confusion is that 0-9 is followed by A-Z which is followed by a-z.
http://www.lookuptables.com/
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
haoest
Participant
1768 Points
404 Posts
Re: how to use RangeValidator for String type?
Nov 19, 2006 06:22 PM|LINK
I did a small test, here's my code:
RangeValidator1.MinimumValue = "0"; RangeValidator1.MaximumValue = "z"; RangeValidator1.ControlToValidate = txt1.ID; string s = ""; for (int i = 0; i < 128; i++) { txt1.Text = ""+ (char)i ; RangeValidator1.Validate(); if (RangeValidator1.IsValid) { s += (char)i + " (" + i + ") is valid<br>"; } }// ASCII( '0' ) = 48
// ASCII( 'z' ) = 122
Guess what the output is?
(9) is valid
(10) is valid
(11) is valid
(12) is valid
(13) is valid
(32) is valid
0 (48) is valid
... '1' to '8' are removed for readability
9 (57) is valid
A (65) is valid
... 'B' to 'X' are valid and removed for readability
Y (89) is valid
[ where did 'Z' go ?? ]
[ and where did the puncutations such as '[', '\', and ']' go? ]
a (97) is valid
... 'b' to 'y' are valid are removed for readability
z (122) is valid
See? it does compare by ascii value, but not totally. I can't find a fitting explaination
A slightly different version of the test, by changing to
the output diffferents significantly. Only characters with ascii value 0 - 32, plus 127, are valid.
WHY so if it compares by ascii value?
Jasson_King
Star
8057 Points
1520 Posts
Re: how to use RangeValidator for String type?
Nov 20, 2006 12:23 AM|LINK
Hi,
I give you a detailed explanation about asp:RangeValidator control for your reference.
The RangeValidator control is used to check that the user enters an input value that falls between two values. It is possible to check ranges within numbers, dates, and characters.
Note:
1.The validation will not fail if the input control is empty. Use the RequiredFieldValidator control to make the field required.
2.The validation will not fail if the input value cannot be converted to the data type specified. Use the CompareValidator control, with its Operator property set to ValidationCompareOperator.DataTypeCheck, to verify the data type of the input value.
3.Specifies the data type of the value to check. The types are:
- Currency
- Date
- Double
- Integer
- String
4.The RangeValidator control throws an exception if the value specified by the MaximumValue or MinimumValue property cannot be converted to the data type specified by the Type property.haoest
Participant
1768 Points
404 Posts
Re: how to use RangeValidator for String type?
Nov 20, 2006 03:43 PM|LINK
The overview about RangeValidator is legitimate, but my question really is technically how it compares the input string against minimiumvalue and maximum value to make sure it's valid.
PLBlum
All-Star
30409 Points
5347 Posts
MVP
Re: how to use RangeValidator for String type?
Nov 20, 2006 04:10 PM|LINK
Here is the exact function used by the client-side validation code to compare two values:
function ValidatorCompare(operand1, operand2, operator, val) {
var dataType = val.type;
var op1, op2;
if ((op1 = ValidatorConvert(operand1, dataType, val)) == null)
return false;
if (operator == "DataTypeCheck")
return true;
if ((op2 = ValidatorConvert(operand2, dataType, val)) == null)
return true;
switch (operator) {
case "NotEqual":
return (op1 != op2);
case "GreaterThan":
return (op1 > op2);
case "GreaterThanEqual":
return (op1 >= op2);
case "LessThan":
return (op1 < op2);
case "LessThanEqual":
return (op1 <= op2);
default:
return (op1 == op2);
}
}
Here is the evaluation function for the RangeValidator.
function RangeValidatorEvaluateIsValid(val) {
var value = ValidatorGetValue(val.controltovalidate);
if (ValidatorTrim(value).length == 0)
return true;
return (ValidatorCompare(value, val.minimumvalue, "GreaterThanEqual", val) &&
ValidatorCompare(value, val.maximumvalue, "LessThanEqual", val));
}
You can see its using the > = < operators in javascript to compare strings. Those work based on ASCII comparisons.
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com
Jasson_King
Star
8057 Points
1520 Posts
Re: how to use RangeValidator for String type?
Nov 21, 2006 02:26 AM|LINK
PLBlum, I agree with you.It does ASCII comparision in the asp:RangeValidator control.
As we have known,the ASCII values of '0' to '9' are 48 to 57, the ASCII values of 'a' to 'z' are 97 to 122 , and the ASCII values of 'A' to 'Z' are 65 to 90.
If we set '0' as minValue and 'z' as maxValue of a asp:RangeValidator control,"0~9","a~z" and "A~Z" should pass validation in the range validator.
PLBlum
All-Star
30409 Points
5347 Posts
MVP
Re: how to use RangeValidator for String type?
Nov 21, 2006 05:48 PM|LINK
If you use those three strings: 0~9, a~z, and A~Z, I would expect them to be within the range.
If you use "z1", it is a "bigger" value than "z" (your max value) and should fail validation.
If you want to experiment, use the CompareValidator to compare just one part of the range, min or max, so you can test individual cases.
<asp:TextBox id="TExtBox1" runat=server /><br />
<asp:TextBox id="TextBox2" runat=server /><br />
<asp:CompareValidator ControlToValidate="TextBox1" ControlToCompare="TextBox2" Type="String" ErrorMessage="wrong" Operator="GreaterThanEqual" />
Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
www.PeterBlum.com