hi there,

i have a mask text box in a form to enter the money amount in the text box.(displayed as $________.__) whee the user enters $___555__.__ how can i validate it.to get the value 555to the database. how ca i do this.

hey anybody has any tutorials regarding validating mask text box??
appreciate if could hlp me

thankxxxxxxxx

Recommended Answers

All 2 Replies

Are you still struggling with MaskedTextBox.
The thing is totaly not fit for purpose. The mask is too restrictive and the user experience is not intuitive when using complex masks.

Why don't you give-up and use the strategy offered to you by [POST=1251316]Radical Edward[/POST] a few day ago.

A slightly longer version of converting your data both ways to/from the server would be in the methods I'll list below. Keep in mind that my 'sample' was drawn up without an actual masked textbox (just straight textbox) but the processes should work if my brain hasn't completely stopped working.

Essentially my example will take a masked value of, say: $___555___.00
change it to an unmasked value of: 555.00
then convert it to a double for ease of conversion to Money datatype when sent to the server.

char[] splits = {'$','_'};
        string maskedValue = maskedTextBox.Text;
        string[] maskedSegs = maskedValue.Split(splits);
        string strippedValue = "";
        for (int a = 0; a < maskedSegs.Length; a++)
        {
            if (maskedSegs[a].Length > 0)
            {
                strippedValue += maskedSegs[a];
            }
        }
        maskStrippedTextBox.Text = strippedValue;
        double toServerValue = Convert.ToDouble(strippedValue);

On the return trip from the server we can immediately convert the returned value to a string as part of the variable population so I skipped that step.

Example checks for the position of the decimal (assuming there is one) and if required adds the appropriate components of .00 to the end of the value returned.
Example then checks the length of the value to determine if it's shorter than the original masked value lengths and if it is (which it should be, having been derived from the masked input) adds $__ with as many "_" as needed to fill to full length before returning the new re-masked value to the box.

//assume that serverValueTextBox is actually the Convert.ToString(serverReturnedValue)
        string serverValue = serverValueTextBox.Text; 
        int decPos = serverValue.IndexOf('.');
        string reMaskedValue = "";
        // if/else here infers that values from server may or may not contain either a decimal or both 0's.  If all values from server are guaranteed to contain .00 (or some other value in the format .##) then you can skip this if/else
        if (decPos == -1) 
        {
             reMaskedValue = serverValue + ".00";
        }
        else
        {
            if ((serverValue.Length - 1) < (decPos + 2))
            {
                if ((serverValue.Length - 1) == decPos)
                {
                    reMaskedValue = serverValue + "0";
                }
                else if (serverValue.Length == decPos)
                {
                    reMaskedValue = serverValue + "00";
                }
            }
        }
        //Assumes original mask to be 13 characters in length $_________.__
        if (reMaskedValue.Length < 13) 
        {
            reMaskedValue = reMaskedValue.PadLeft(12, '_');
            reMaskedValue = reMaskedValue.PadLeft(13, '$');
        }
        reMaskedTextBox.Text = reMaskedValue;

And if that doesn't answer both of your posts in one shot I don't know what does because the other dude in your other post gave you a MUCH simpler way of doing it before :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.