I have a series of text boxes on my form, and my client wants me to filter out characters that aren't allowed, for example in the name
field you cannot have symbols or numbers.
Now, he wants it so when you try and put in a special character it simply will not get entered into the text box. I know the logistics to this, but I'm not sure how I would go about coding it.
Basically what needs to happen is when the user types in characters like $, ^, 5, *
etc, a function needs to recognise this and stop them from being entered into the textbox, whether it means deleting them as soon as they go in or interrupting the action altogether.
Anybody have some insight into this? Anything is appreciated, thanks.
You could use a regex:
The actual
Regex
ought to be made a member of the form so it isn't declared each time or placed into some common class for reference. The selection logic is used to keep the cursor at its current location after stripping the invalid characters.For WinForms you can use the MaskedTextBox Class and set the Mask property.
For ASP.NET you could use the AJAX Toolkit's MaskedEdit control.