Click here to Skip to main content
15,879,490 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
XML
This is my masked textbox from the WPF Extended Toolkit 

<toolkit:MaskedTextBox Name="MaskedtxtTaxId"  Mask="00-0000000" />


I don't want the user to enter incomplete value eg 45-781____
but it can be empty like __-_______
I need to force the user to enter all 9 digits or leave it empty.
Can I accomplish that with the MaskedTextBox?

Thank you.

Posted
Updated 10-Apr-13 6:17am
v4

try this.
C#
private void MaskedtxtTaxId_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
  if (((MaskedTextBox)sender).MaskedTextProvider.AssignedEditPositionCount < ((MaskedTextBox)sender).MaskedTextProvider.AvailableEditPositionCount)
  {
    System.Windows.MessageBox.Show("Not valid");
    /// do something
  }
}
 
Share this answer
 
v2
Comments
_Natula 10-Apr-13 13:36pm    
Thank you- your code treat blank string as "Not Valid".this field is not required field.

This one works but i did not like it:-
var vPromptCharCount = MaskedtxtTaxId.Text.Count(x => x == '_');
if (count > 0 && count < 9)
{
//invalid
}
stibee 10-Apr-13 13:44pm    
Sorry I do not understand what you mean. What do you really wanna do? If you write this field into a database, than only write it in the case the string is valid other wise you can ignore it.
_Natula 10-Apr-13 13:59pm    
let say I want prompt a message box says "Not Invalid".The code u provided pop up "Not Valid" for the String.Empty at the lost focus event.I don,t want to show "Not Valid" message for string.empty; Thanks for your time
stibee 10-Apr-13 14:04pm    
Ok sorry for confusing... this was only for my tests;-)
You need to check the Length of Value entered in the MasketTextBox.

Something like

VB
If ( MaskedtxtTaxId.Value.Length < 9 and  MaskedtxtTaxId.Value.Length > 0 ) Then

 'display message "Value entered cannot be incomplete"

End if
 
Share this answer
 
v3
Comments
_Natula 10-Apr-13 12:59pm    
I have tried that,every time the length is 10 because its counting PhrompCar and the hiphen(__-_______). I also changed the PhrompCar to "space",still the length is 10.
C#
var vPromptCharCount = MaskedtxtTaxId.Text.Count(x => x == '_');
if (count > 0 && count < 9)
{
    //invalid
 }
 
Share this answer
 
v3

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