0

Possible Duplicate:
How do I replace multiple spaces with a single space in C#?

I have a TextBox in which user is going to enter search Criteria in TEXTBOX. Now, i want to prevent the user from entering NOT MORE THAN ONE SPACE AFTER EACH WORD in TEXTBOX. How to do it in C#.

Thanks in Advance!

Community
  • 1
  • 1
SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143
  • Your question tells us what you want to implement, but what is the actual business requirement? There are a couple of ways to approach this, of which restricting the user's entry is probably the hardest and most cumbersome. – slugster Jul 11 '12 at 10:25

1 Answers1

3

If you are looking to do it via the server side you could do a simple regex that would replace any double spaces with a singular space

RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"[ ]{2,}", options);     
MyTextBox.Text= regex.Replace(MyTextBox.Text, @" ");

If you wan to do it on the client side you can still use the same regex but do it on JavaScript and fire it on the onBlur event

John Mitchell
  • 9,653
  • 9
  • 57
  • 91