I am trying to accept as input a Zip code with the optional Zip extension (ie. 12345-1234).
So my InputMask looks like: 99999-9999
I use 9 instead of # because I want the values to optional. What I was wondering is if there was a way for me to retrieve the value from the control including the hyphen, but to only include the hyphen if a Zip extension was provided. I currently have the DataMode property set to IncludeLiterals, which will give me the following
Entering 12345 gives me 12345- (This is what I am trying to avoid)
Entering 12345-1234 gives me 12345-1234
The way that my code is written, the postal code / zip field is dynamically provided an InputMask given a specific Country code. Therefore, I cannot write specific code with the knowledge of it being a Zip code.
The concept that I am looking for would also accomplish the following. Providing an InputMask of (999) 999-9999 Ext.99999, when entering a value of 1231231234, I would like a resulting value of (123) 123-1234 (excluding the extension). Or more likely, if the user does not enter any data at all for the resulting value to be completely empty.
I guess you could describe the feature as "Include Literals If Data Follows".
Any Ideas?
(Using 2008.2)
I think I may have a home grown solution. It seems to work with a few examples.
The key was to have the control's DataMode property set to IncludeBoth (prompt and literal chars) so that they could then be matched one-to-one against the mask.
Any thoughts or suggestions?
class Program
{
static void Main(string[] args)
Console.WriteLine(ScrubMaskedData_Text("999-999-9999 Ext 99999>", "123-456-7890 Ext _____", '_')); // phone # w/ext
Console.WriteLine(ScrubMaskedData_Text(">?9? 9?9>", "L4N ___", '_')); // canadian postal code
Console.WriteLine(ScrubMaskedData_Text("99999-9999>", "58282-1234", '_')); // zip code w/ext
Console.WriteLine(ScrubMaskedData_Text("99999-9999>", "58282-____", '_'));
Console.WriteLine(ScrubMaskedData_Text("99999-9999>", "58___-____", '_'));
}
public static char[] m_MaskChars = new char[] { '#', '&', 'A', 'a', '9', 'C', '?', 'n' };
public static string ScrubMaskedData_Text(string sMask, string sValue, char cPromptChar)
// if the last char is a greater than sign (which would normally mean uppercase in the mask), then continue on to scrub the data
if (!sMask.EndsWith(">"))
return sValue;
// remove non-placeholder mask chars
sMask = sMask.Replace("<", string.Empty).Replace(">", string.Empty);
// replace mask chars marked as literals with a pipe
StringBuilder sb = new StringBuilder(sMask.Length);
for (int ichar = 0; ichar < sMask.Length; ichar++)
char cCur = sMask[ichar];
if (cCur == '\\' && ichar < sMask.Length - 1)
ichar++;
cCur = sMask[ichar];
else if (Array.IndexOf(m_MaskChars, cCur) > -1)
cCur = '|';
sb.Append(cCur);
sMask = sb.ToString();
int iDataFoundAt = 0;
for (int ichar = sMask.Length - 1; ichar >= 0; ichar--)
char cMask = sMask[ichar];
char cVal;
if (sValue.Length - 1 >= ichar)
cVal = sValue[ichar];
if (cMask == '|' && ichar > iDataFoundAt && cVal != cPromptChar)
iDataFoundAt = ichar;
break;
if (iDataFoundAt == 0)
return string.Empty;
if (iDataFoundAt + 1 > sValue.Length)
return sValue.Substring(0, iDataFoundAt+1);