textbox and regex limitation ?

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 5841 Views - Last Post: 11 November 2009 - 04:57 PM Rate Topic: -----

#16 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: textbox and regex limitation ?

Posted 11 November 2009 - 09:49 AM

You do know that validation doesn't take place until you finish. If you want to validate as they type, you'll have to attach a method to the keypress event and filter out what you don't want.
Was This Post Helpful? 0
  • +
  • -

#17 efficacious   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 09-November 09

Re: textbox and regex limitation ?

Posted 11 November 2009 - 10:44 AM

Your right.. I'm just trying to help you accomplish the goal and wanted to get across the fact that you may be dealing with all the trouble for no reason.

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

Was This Post Helpful? 0
  • +
  • -

#18 [DuM]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-November 09

Re: textbox and regex limitation ?

Posted 11 November 2009 - 11:26 AM

I can write with a few lines but i want to write the following as always.
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.
Was This Post Helpful? 0
  • +
  • -

#19 efficacious   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 09-November 09

Re: textbox and regex limitation ?

Posted 11 November 2009 - 11:31 AM

so maybe this code?

//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

Was This Post Helpful? 0
  • +
  • -

#20 [DuM]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-November 09

Re: textbox and regex limitation ?

Posted 11 November 2009 - 12:21 PM

This line working only...
//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 :crazy: .

This post has been edited by [DuM]: 11 November 2009 - 12:21 PM

Was This Post Helpful? 0
  • +
  • -

#21 efficacious   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 09-November 09

Re: textbox and regex limitation ?

Posted 11 November 2009 - 12:28 PM

Sory I don't understand what you mean by any number??

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

Was This Post Helpful? 0
  • +
  • -

#22 [DuM]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-November 09

Re: textbox and regex limitation ?

Posted 11 November 2009 - 01:55 PM

I mean any number between 0-9, all possible numbers :D. No i don't said that. Number betwen 1-24 which means 1,2,3,4...24.
Was This Post Helpful? 0
  • +
  • -

#23 efficacious   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 09-November 09

Re: textbox and regex limitation ?

Posted 11 November 2009 - 02:25 PM

I'm just getting more confused here.

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

Was This Post Helpful? 0
  • +
  • -

#24 [DuM]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-November 09

Re: textbox and regex limitation ?

Posted 11 November 2009 - 02:50 PM

Seems to be ok but not working. I can limit textbox only number with length of two number at easier way. Can to limit this only with regex or not ? Tnx anyway slowly i give up it seems that it is impossible to do only with regex.
Was This Post Helpful? 0
  • +
  • -

#25 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: textbox and regex limitation ?

Posted 11 November 2009 - 03:40 PM

Why not use a NumericUpDown control? It allows users to input values just like a text box, but it only allows numbers and you can set the minimum and maximum values allowed.

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

Was This Post Helpful? 0
  • +
  • -

#26 [DuM]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-November 09

Re: textbox and regex limitation ?

Posted 11 November 2009 - 04:13 PM

Yes i try with keypress event and Momerath's regex but now work in C#.

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.
Was This Post Helpful? 0
  • +
  • -

#27 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: textbox and regex limitation ?

Posted 11 November 2009 - 04:44 PM

Now I can see your problem.

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

Was This Post Helpful? 1
  • +
  • -

#28 [DuM]   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-November 09

Re: textbox and regex limitation ?

Posted 11 November 2009 - 04:57 PM

Tnx all for work ;).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2