Hi,
i need to block input if first char 2 and second char must not be more 5. And first char input must be 1 or 2. Specifically I need the input of only 1-25 and nothing else. I tried and only allow entry of numbers, but fail to limit specified .
Maybe you can do something with this http://www.daniweb.com/code/snippet217084.html.
There are other related threads here but I think you are smart enough to search this site and find out for yourself.
//using this rather than TextBox.Text.Length ensures
//correct behaviour if user selects and overwrites contents of textbox
int len = TextBox.Text.Length - TextBox.SelectionLength;
//default to true then change to false if valid value entered
e.Handled = true;
switch(len)
{
//allow 1 or 2 as first character
case0:
if(e.KeyChar == '1' || e.KeyChar == '2')
e.Handled = false;
break;
//if this is the second character entered
case1:
//check where user is typing. If user entered a number then moved the cursor back to start
//of textbox then still only allow a 1 or 2.
if(TextBox.SelectionStart == 0)
{
if(e.KeyChar >= '1' && e.KeyChar <= '2')
e.Handled = false;
}
//if the user is entering the second digit then allow 0 to 5
else
{
if(e.KeyChar >= '0' && e.KeyChar <= '5')
e.Handled = false;
}
break;
default:
break;
}
}
}
This code will ensure that only numebrs from 1 to 25 can be ntered. However, there is a limitation to this solution; it will not prevent the user from PASTING invalid data into the textbox, it will only prevent them typing incorrect values.
Remember to mark the thread as solved if this has answered your question
Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.
This thread is more than three months old
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.