Click to See Complete Forum and Search --> : Restricting data values
Bill Crawley
November 3rd, 2005, 03:39 AM
HI All,
On a web form, what code do I need to ensure only numeric values are entered and what event should I place it in if I have a textbox for entry and autopostack is set to true for that control?
HanneSThEGreaT
November 3rd, 2005, 04:01 AM
Hi Bill!
Here's a couple of thoughts...
1) If you need to validate numeric data, for example, an age, perform type checks using the int type. To convert string input to integer form you can use Int32.Parse or Convert.ToIn32, and then handle any FormatException that occurs with an invalid data type, as follows:
try
{
int i = Int32.Parse(txtAge.Text);
. . .
}
catch( FormatException)
{
. . .
}
2) The following example shows the HTML code that is generated for a RegularExpressionValidator control that has been used to validate a U.S. social security number form field:
<form id="WebForm" method="post" runat="server">
<asp:TextBox id="txtSSN" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator id="ssnRegex" runat="server"
ErrorMessage="Invalid social security number"
ValidationExpression="\d{3}-\d{2}-\d{4}"
ControlToValidate="txtSSN">
</asp:RegularExpressionValidator>
</form>
The preceding validation expression is one of the standard expressions that Visual Studio .NET provides. It validates the format of the supplied input field as well as its type and length. The input must consist of three numeric digits followed by a dash, then two digits followed by a dash, and then four digits.
3) Sometimes you need to validate that input data falls within a predetermined range. The following code uses an ASP.NET RangeValidator control to constrain input to whole numbers between 0 and 255. This example also uses the RequiredFieldValidator. Without the RequiredFieldValidator, the other validator controls accept blank input.
<form id="WebForm3" method="post" runat="server">
<asp:TextBox id="txtNumber" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
id="rangeRegex"
runat="server"
ErrorMessage="Please enter a number between 0 and 255"
ControlToValidate="txtNumber"
style="LEFT: 10px; POSITION: absolute; TOP: 47px" >
</asp:RequiredFieldValidator>
<asp:RangeValidator
id="RangeValidator1"
runat="server"
ErrorMessage="Please enter a number between 0 and 255"
ControlToValidate="TextBox1"
Type="Integer"
MinimumValue="0"
MaximumValue="255"
style="LEFT: 10px; POSITION: absolute; TOP: 47px" >
</asp:RangeValidator>
<asp:Button id="Button1" style="LEFT: 10px; POSITION: absolute; TOP: 100px"
runat="server" Text="Button"></asp:Button>
</form>
The following example shows how to validate range using the Regex class:
try
{
// The conversion will raise an exception if not valid.
int i = Convert.ToInt32(sInput);
if ((0 <= i && i <= 255) == true)
{
// data is valid, use the number
}
}
catch( FormatException )
{
. . .
}
Especially number 3 is very useful..
Will this help you ¿
DivyaJaiKumar
November 9th, 2005, 02:18 AM
Hai,
In asp if you are using javascript U can use the following coding to check if you have to enter only the numeric datas:
p=document.form.Name.value;
i=0;
while(i<p.length)
{
ch=p.charAt(i);
if((p.length==0) || (ch>'0' && ch<'9') || (ch == '!') || (ch == '@') || (ch = '#') || (ch == '$') || (ch == '%') || (ch == '^') || (ch == '&')
|| (ch == '*') || (ch == '(') || (ch == ')') || (ch == '-') || (ch == '+') || (ch == '=') || (ch == '|') || (ch == '_')
|| (ch == '`') || (ch == '~'))
{
alert("Please enter the UserName and also Data should not have numerals");
document.form.Name.focus();
return false;
}
i++;
}
Try this it may be usefull.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.