This is a migrated thread and some comments may be shown as answers.

I have a RadMaskedTextBox, and want to verify an arbitrary string against the mask on the server side.

5 Answers 204 Views
Input
This is a migrated thread and some comments may be shown as answers.
Robert Columbia
Top achievements
Rank 1
Robert Columbia asked on 27 Feb 2010, 09:35 PM
Okay, here we go.

On the *server side*, I have :

a RadMaskedTextBox, which has an arbitrary Mask that is set by someone else.
a string, which contains an arbitrary string that is set by someone else.

I want to determine whether or not this string would validate inside the RadMaskedTextBox given its Mask.

I determined experimentally that attempting to stuff a non-validating value into the Text property (e.g. by saying myBox.Text = <some string that wouldn't validate) of the box results in no change to the current value, so I could come up with a solution based on that, but that seems kind of messy and may puzzle the next developer.

I could attempt to write my own mask parser and validator, though that seems like it would be risky.

Any ideas?

5 Answers, 1 is accepted

Sort by
0
Robert Columbia
Top achievements
Rank 1
answered on 27 Feb 2010, 09:57 PM
And, how does MaskParts fit into this?  I could clarify my question by asking whether or not there is a predicate method that I can call that will tell me whether or not a given string would validate against the box.
0
Veli
Telerik team
answered on 01 Mar 2010, 04:53 PM
Hello Robert,

The easiest way for you is to create a Regex object with the Mask of the RadMaskedTextBox and check your string:

//RadMaskedTextBox1 is the RadMaskedTextBox instance
//myString is the custom string you need to validate
 
Regex regex = new Regex(RadMaskedTextBox1.Mask);
if(regex.IsMatch(myString))
{
    //string validates
}
else
{
    //string does not validate
}


Best wishes,
Veli
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Surath
Top achievements
Rank 1
answered on 03 Aug 2010, 03:44 AM
Hi,
The code does not work for me. I use the RadMaskedText box with a mask like "999-99-9999" and when I enter a value such as "111-11-1111" or any other valid SSN format strings the regexobject.IsMatch() method returns false always.

 

 

Regex regex = new Regex(mask);

 

 

 

 

if (regex.IsMatch(mytext))

 

{

 

 

    return true;

 

}

 

 

return false;

Any suggestions?

Surath.

 

0
Veli
Telerik team
answered on 04 Aug 2010, 04:54 PM
Hello Surath,

A mask of "999-99-9999" is not valid for RadMaskedTextBox. Check this help topic on what special characters are supported in the mask. The mask that would accept a value of "111-11-1111" is "###-##-####".

Once you get acquainted with the rules for building masks for RadMaskedTextBox, you can use a method that will translate the mask into a regular expression:

using System;
using Telerik.Web.UI;
using System.Text.RegularExpressions;
using System.Text;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RadMaskedTextBox box = new RadMaskedTextBox();
        box.ID = "RadMaskedTextBox1";
        Page.Form.Controls.Add(box);
 
        box.Mask = "###-##-####";
        string text = "111-11-1111";
 
        //box.Mask = "aAL.l#<2..8><nick|mike|john>";
        //string text = "+ADv43mike";
        box.Text = text;
 
        ResponseWrite(box.Mask, text, GetRegexFromMaskParts(box.MaskParts));
    }
 
    private void ResponseWrite(string mask, string value, Regex regex)
    {
        StringBuilder sb = new StringBuilder();
 
        sb.AppendLine("<table class='t'>");
        sb.AppendLine(String.Format("<tr><td>Mask</td><td>{0}</td></tr>", mask));
        sb.AppendLine(String.Format("<tr><td>Regex</td><td>{0}</td></tr>", regex.ToString()));
        sb.AppendLine(String.Format("<tr><td>Value</td><td>{0}</td></tr>", value));
        sb.AppendLine(String.Format("<tr><td>Match</td><td>{0}</td></tr>", regex.IsMatch(value)));
        sb.AppendLine("</table>");
 
        Response.Write(sb.ToString());
    }
 
    private Regex GetRegexFromMaskParts(MaskPartCollection maskParts)
    {
        StringBuilder sb = new StringBuilder();
        foreach (MaskPart part in maskParts)
        {
            if (part is DigitMaskPart)
            {
                sb.Append(@"\d");
            }
 
            if (part is FreeMaskPart)
            {
                sb.Append(".");
            }
 
            if (part is LiteralMaskPart)
            {
                sb.Append(((LiteralMaskPart)part).Text);
            }
 
            if (part is LowerMaskPart)
            {
                sb.Append("[a-z]");
            }
 
            if (part is UpperMaskPart)
            {
                sb.Append("[A-Z]");
            }
 
            if (part is NumericRangeMaskPart)
            {
                NumericRangeMaskPart numRange = (NumericRangeMaskPart)part;
                sb.Append(String.Format("[{0}-{1}]", numRange.LowerLimit, numRange.UpperLimit));
            }
 
            if (part is EnumerationMaskPart)
            {
                EnumerationMaskPart enumPart = (EnumerationMaskPart)part;               
                sb.Append(String.Format("("));
                for(var i =0; i < enumPart.Items.Count; i++)
                {
                    sb.Append(enumPart.Items[i]);
                    if (i < enumPart.Items.Count - 1)
                    {
                        sb.Append("|");
                    }
                }
                sb.Append(String.Format(")"));
            }
        }
 
        return new Regex(sb.ToString());
    }
}


The above shown GetRegexFromMaskParts() method translates a MaskPartsCollection into a Regex. RadMaskedTextBox builds a collection of mask parts both for its Mask and DisplayMask. These mask parts represent the parsed masked in a convenient usable form. You can use the MaskPartCollection you need to build a regular expression out of its mask parts. You can then use the Regex to match a specific value.

Attached is the test page demonstrating this method.

Regards,
Veli
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Silvio Silva Junior
Top achievements
Rank 2
answered on 11 Dec 2013, 02:49 PM
Thanks Veli!

Works like a charm!

Best regards.

Tags
Input
Asked by
Robert Columbia
Top achievements
Rank 1
Answers by
Robert Columbia
Top achievements
Rank 1
Veli
Telerik team
Surath
Top achievements
Rank 1
Silvio Silva Junior
Top achievements
Rank 2
Share this question
or