Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 10-Apr-13 7:08am
_Natula879
Edited 10-Apr-13 7:17am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

try this.
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
  }
}
  Permalink  
v2
Comments
_Ethio at 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 at 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.
_Ethio at 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 at 10-Apr-13 14:04pm
   
Ok sorry for confusing... this was only for my tests;-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to check the Length of Value entered in the MasketTextBox.
 
Something like
 
If ( MaskedtxtTaxId.Value.Length < 9 and  MaskedtxtTaxId.Value.Length > 0 ) Then
 
 'display message "Value entered cannot be incomplete"

End if
  Permalink  
v3
Comments
_Ethio at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

 var vPromptCharCount = MaskedtxtTaxId.Text.Count(x => x == '_');
 if (count > 0 && count < 9)
 {
     //invalid
  }
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 275
1 Maciej Los 230
2 Sergey Alexandrovich Kryukov 185
3 Afzaal Ahmad Zeeshan 152
4 ProgramFOX 130
0 OriginalGriff 6,524
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,218
3 Manas Bhardwaj 4,657
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100