Click here to Skip to main content
15,905,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'v a textbox and it should accept only numbers entered from 0 to 366.
Not any other things...
Posted
Comments
Pawan Kiran 3-Nov-10 8:03am    
if u Tested my Answer, Leave some comment.

You may add the following delegate for the validate event:

C#
private void Validate_Text(object sender, CancelEventArgs e)
{
    TextBox tb = sender as TextBox;
    if (tb != null)
    {
        int i;
        if (int.TryParse(tb.Text, out i))
        {
            if (i >= 0 && i <= 366)
                return;
        }
    }
    MessageBox.Show("invalid input");
    e.Cancel = true;
}


Of course you will provide a better (more informative) message for the user.
:)
 
Share this answer
 
at the keypress event of textbox write

if (!(e.KeyChar >= '0' && e.KeyChar <= '9'))
                e.Handled = true;

it will secure to enter only numbers.
and as i getting your second question is about limitation of numbers enetered in Textbox. for this you can use MaxLength property of textnox
 
Share this answer
 
Comments
Suthish Nair 2-Nov-10 5:59am    
MaxLength. how this will work, can you explain?
RaviRanjanKr 2-Nov-10 8:02am    
why not,I will explain it by using MaxLength property of textbox control as
textBox1.MaxLength = 100 // always integer value
because of MaxLength, textbox can contain upto define value.
Suthish Nair 2-Nov-10 8:47am    
"it should accept only numbers entered from 0 to 366"

greater than 0 and less than 366

MaxLength property will set length of textbox..
RaviRanjanKr 2-Nov-10 8:58am    
yes, MaxLength set the Maximum length of textbox..
textbox.MaxLength [ Returns the maximum number of characters allowed in the text box. The default value is 0, meaning that there is no limit on the length]
How about you use NumericUpdown Control[http://msdn.microsoft.com/en-us/library/729xt55s.aspx[^]]? It does what you want. It has Minimum, Maximum, Skip properties, and takes in a number.
 
Share this answer
 
Just put a condition on the content.


C#
bool contLetter = false;
string reqNumber = textBox1.Text.Trim();
for (int i = 0; i < reqNumber.Length; i++)

{
if (!char.IsNumber(reqNumber[i]))
{
contLetter = true;
}
}
if (contLetter)
{
// Some error label and redo enter textbox value
}


Then once you have confirmed a number, confirm it's value is between the chosen ranges.

(I leave that as an excercise for the OP)
 
Share this answer
 
Take a look at this
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (char.IsDigit(e.KeyChar) == true)
    {
        e.Handled = false;
        if (textBox1.Text.Length > 0)
        {
            if (Convert.ToInt32(textBox1.Text) >= 36)
            {
                if (Convert.ToInt32(textBox1.Text) >= 37)
                {
                    textBox1.MaxLength = 2;
                }
                else
                {
                    textBox1.MaxLength = 3;
                }
                if (Convert.ToInt32(e.KeyChar.ToString()) > 6)
                {
                    e.Handled = true;
                    return;
                }
                else
                {
                    e.Handled = false;
                }
            }
            else
            {
                e.Handled = false;
                textBox1.MaxLength = 3;
            }
        }
        else
        {
            e.Handled = false;
        }
    }
    if (e.KeyChar == (char)Keys.Back || char.IsDigit(e.KeyChar) == true)
    {
        e.Handled = false;
    }
    else
    {
        e.Handled = true;
    }
}


Vote if is Useful.
 
Share this answer
 
try..

C#
function funNumber(a) {
    if (a.value > 366) {
        a.value = a.value.substring(0, 3);
        return true;
    }
    else {
        if (event.keyCode >= 48 && event.keyCode <= 57) {
            return true;
        }
        else if (event.keyCode >= 45 && event.keyCode <= 46) {
        }
        else {
            event.returnValue = false;
            return false;
        }
    }
}



on page load

C#
protected void Page_Load(object sender, EventArgs e)
   {
       txtUserName.Attributes.Add("onkeyup", "funNumber(this)"); 
}
 
Share this answer
 
Comments
Toli Cuturicu 2-Nov-10 9:24am    
function is not a keyword in c#.
event keyword is not used in this way.
What page??
Suthish Nair 3-Nov-10 2:05am    
ok.. i taught it was an web application because the tag is C# only (Windows or Web?)...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900