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

6 posts, 0 answers
  1. Robert Columbia
    Robert Columbia avatar
    14 posts
    Member since:
    Jun 2012

    Posted 27 Feb 2010 Link to this post

    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?
  2. Robert Columbia
    Robert Columbia avatar
    14 posts
    Member since:
    Jun 2012

    Posted 27 Feb 2010 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 01 Mar 2010 Link to this post

    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.
  5. Surath
    Surath avatar
    1 posts
    Member since:
    Sep 2008

    Posted 02 Aug 2010 Link to this post

    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.

     

  6. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 04 Aug 2010 Link to this post

    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
  7. Silvio Silva Junior
    Silvio Silva Junior avatar
    100 posts
    Member since:
    Mar 2010

    Posted 11 Dec 2013 Link to this post

    Thanks Veli!

    Works like a charm!

    Best regards.

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017