none
MaskedTextProvider doesn't match mask exactly RRS feed

  • Question

  • Hi,

    I want to verify if a string follows a mask exactly. The mask is 99.99.99-999-99.

    I have the following code:

    MaskedTextProvider maskProvider = new MaskedTextProvider( "99.99.99-999-99",CultureInfo.CurrentCulture );
    
    if( !maskProvider.VerifyString("12345678912") ) {
           //throw some error
    }
    

    For some reason this string is regarded as valid input when it shouldn't. "12.34.56-789-12" is regarded as invalid input.

    This should be the other way around. What am I missing?

     

    Tuesday, August 17, 2010 6:35 AM

Answers

  • MaskedTextProvider is for input. The user is allowed to only input the digit and is not required to type the dots and dashes. The MaskedTextProvider sees the dots as decimal separators. If your current culture doesn't use the dot as decimal separator, that could explain why "12.34.56-789-12" is not valid.

    To verify that a string matches a given format, a regex is more suited.

    If you want to use the MaskedTextProvider for that, you need to use an InvariantCulture, verify that the string is a valid input for the mask and that its length is the same as the mask.

    • Proposed as answer by Nancy Shao Wednesday, August 18, 2010 8:03 AM
    • Marked as answer by SilentBob101 Wednesday, August 18, 2010 8:15 AM
    Tuesday, August 17, 2010 3:37 PM