Remove double spaces in a textbox on keypress
-
I have this textbox that users enter their names, but I realized that they enter their names by entering so many spaces. I used the replace function but it does not work well. Is there anyway to prevent this at keypress, when the user tries to press the space bar more than once? Thank you, all.
-
I have this textbox that users enter their names, but I realized that they enter their names by entering so many spaces. I used the replace function but it does not work well. Is there anyway to prevent this at keypress, when the user tries to press the space bar more than once? Thank you, all.
I write a code in c#.. hope will you get help from this (You can use its logic in VB)... http://hemantrautela.blogspot.com/2012/09/trimming-string-cnet-aspnet.html[^]
-
I have this textbox that users enter their names, but I realized that they enter their names by entering so many spaces. I used the replace function but it does not work well. Is there anyway to prevent this at keypress, when the user tries to press the space bar more than once? Thank you, all.
Member 11078565 wrote:
I used the
replace function but it does not work well.What does that mean? Did you use it correctly?
yourString = yourString.Replace(" ", " ")
or with Regular Expressions:
yourString = System.Text.RegularExpressions.Regex.Replace(yourString,"\\s+"," ")
-
I have this textbox that users enter their names, but I realized that they enter their names by entering so many spaces. I used the replace function but it does not work well. Is there anyway to prevent this at keypress, when the user tries to press the space bar more than once? Thank you, all.
Trap the keydown/up event, inspect the current and previous characters in the string, if they are both a space then deal with it in the event.
Never underestimate the power of human stupidity RAH