Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
>Help me any one ,

My requirment is textbox must allow only 1 to 9 and one DOT (.) , this should be done by REGULAR EXPRESSION. <b>IN Any position of the text box should not allow alphabets</b>
like 44rt.56rt / R45.9t9

If the above way is not possible kindly advice any alternate way to accomplish this.

Note: Iam using MVVM patern with Silverlight , iam not much intested to write any Text Box events at code behind.

Regards

Srilatha
Posted
Updated 23-Oct-13 5:03am
v3
Comments
Sergey Alexandrovich Kryukov 23-Oct-13 15:46pm    
Either tell us your requirements, or tell us what did you use, not both things together. I hope you understand why.
—SA
Irina Pykhova 24-Oct-13 6:58am    
if you don't want events, you need some advanced TextBox control which supports masking

1 solution

It has nothing to do with Regular Expression. You need to handle the event System.Windows.UIElement.TextInput. This event can be canceled, which is done by assigned true to the property System.Windows.RoutedEventArgs.Handled (runtime type will be different, see below) passed to your event handler. You will need to check up the value of the property System.Windows.TextCompositionEventArgs.Text, also passed in the same event arguments object and see if it is good for input or not. If not, assign true to Handled, to ignore the change. This text is supposed to be just one character the user tries to type.

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.textinput%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventhandler.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventargs.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.routedeventargs.handled.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.input.textcompositioneventargs.text.aspx[^].

Something like:
C#
myTextBox.TextInput += (sender, eventArgs) => {
   bool isGoodInput = CheckIfYouWantThisInput(eventArgs.Text); // don't forget to allow #8 (backspace) which is also considered as text, by some historical reasons
   eventArgs.Handled = !isGoodInput; // ignore then
};


—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900