27 Replies - 5841 Views - Last Post: 11 November 2009 - 04:57 PM
#16
Re: textbox and regex limitation ?
Posted 11 November 2009 - 09:49 AM
#17
Re: textbox and regex limitation ?
Posted 11 November 2009 - 10:44 AM
Maybe you could just run a regex to search for digits only numbers 0-9, 1-2 digits in length.
Then run a seperate if checking to see if the number is atleast greater than 1 and still less than 24?
//Possible regex code
string Patt = '^[0-9]{1,2}$';
string validFlag = "invalid";
//I think we need to convert the txt box string to Int for this.
int txtBoxValue = Int.Parse(txtBox.Text);
//Sorry I am new to C# so I don't know exactly the code for regex
if(/*SomeRegexCode*/)
{
if(txtBoxValue >= 1 && txtBoxValue <= 24)
{
validFlag = "valid";
//or Run some other code w/e
}
}
This post has been edited by efficacious: 11 November 2009 - 11:07 AM
#18
Re: textbox and regex limitation ?
Posted 11 November 2009 - 11:26 AM
Just do not know write regex with this example. Is not it better like this?
if(!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "^(\\d|1\\d|2[0-4])$"))
{
e.Handled = true;
}
Only regex not work.
#19
Re: textbox and regex limitation ?
Posted 11 November 2009 - 11:31 AM
//I think we need to convert the txt box string to Int for
//the second if check
int txtBoxValue = Int.Parse(txtBox.Text);
//Sorry I am new to C# so I don't know exactly the code for regex
if(!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "^[0-9]{1,2}$"))
{
if(txtBoxValue >= 1 && txtBoxValue <= 24)
{
e.Handled = true;
}
}
This post has been edited by efficacious: 11 November 2009 - 11:54 AM
#20
Re: textbox and regex limitation ?
Posted 11 November 2009 - 12:21 PM
//I think we need to convert the txt box string to Int for //the second if check int txtBoxValue = Int.Parse(txtBox.Text);
//I think we need to convert the txt box string to Int for //the second if check int txtBoxValue; int.TryParse(txtBox.Text, out txtBoxValue);
But again allow to type any number
This post has been edited by [DuM]: 11 November 2009 - 12:21 PM
#21
Re: textbox and regex limitation ?
Posted 11 November 2009 - 12:28 PM
To me any number is a numerical digit between 0 and infinite.
But then you say that you only want the numbers 1-24?
Can you elaborate on this?
This post has been edited by efficacious: 11 November 2009 - 12:31 PM
#22
Re: textbox and regex limitation ?
Posted 11 November 2009 - 01:55 PM
#23
Re: textbox and regex limitation ?
Posted 11 November 2009 - 02:25 PM
For any number AKA (0-9)
the regex is
//This is only 1 digit in length "^[0-9]$"
For two digits:
"^[0-9]{2}$"
For one or two digit lengths:
"^[0-9]{1,2}$"
So if a user types in any number between 0-99 then it passes the regex validation...
Once we then add the following embedded IF it narrows the numbers down further to only 1-24
If (TextBoxValue >= 1 && TextBoxValue <= 24)
{
//If you've gotten to this point it means the
//user typed a number in the text box that is between 1-24
}
This post has been edited by efficacious: 11 November 2009 - 02:26 PM
#24
Re: textbox and regex limitation ?
Posted 11 November 2009 - 02:50 PM
#25
Re: textbox and regex limitation ?
Posted 11 November 2009 - 03:40 PM
Really, the only big difference between a NumericUpDown control and a TextBox is the spinner.
Also, how are you checking the user's input? Momerath's regex will work, I just tested it!
This post has been edited by lesPaul456: 11 November 2009 - 03:50 PM
#26
Re: textbox and regex limitation ?
Posted 11 November 2009 - 04:13 PM
if(!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "^(\\d|1\\d|2[0-4])$"))
{
e.Handled = true;
}
If regex there is no possibility to limit this i will use NumericUpDown.
#27
Re: textbox and regex limitation ?
Posted 11 November 2009 - 04:44 PM
It has nothing to do with the regex. It's how your handling it.
In the KeyDown event, use Momerath's regex. Instead of using the Handled property, use the SuppressKeyPress property.
If you set Handled to true on a TextBox, that control will not pass the key press events to the underlying Win32 text box control, but it will still display the characters that the user typed.
Setting SuppressKeyPress to true will prevent the characters from being shown and will set Handled to true.
This post has been edited by lesPaul456: 11 November 2009 - 04:45 PM
#28
Re: textbox and regex limitation ?
Posted 11 November 2009 - 04:57 PM

New Topic/Question
Reply



MultiQuote



|