MaskedTextInput: Prevent entering text with newlines

3 posts, 0 answers
  1. bitbonk
    bitbonk avatar
    10 posts
    Member since:
    Apr 2012

    Posted 30 Jun Link to this post

    How can I prevent the user from entering or pasting text that contains newlines in a RadMaskedTextInput. The user should also not be able to enter a newline using Ctrl+Return or Shift+Return or Alt+Return. 

    Setting the `AcceptsReturn` property to `false` has no effect at all. And its documentation states

    > "Gets or sets a value indicating whether newline is accepted when the mask supports multiline."

    Unfortunately the docs fail to mention how to set set a mask that prevents multiline.

  2. bitbonk
    bitbonk avatar
    10 posts
    Member since:
    Apr 2012

    Posted 30 Jun in reply to bitbonk Link to this post

    The reason that "AcceptsReturn" had no effect was because I had overridden the control template, that part works now. 

    However the user can still paste multiline text. How can I prevent this without having to subclass the control or using code behind.

  3. Petar Mladenov
    Admin
    Petar Mladenov avatar
    3143 posts

    Posted 02 Jul Link to this post

    Hi Jim,

    Yes, there is a difference in behavior between TextBox and RadMaskedTextInput when multiline text is pasted. It is hard for me to determine whether this is 100% bug in our control since this is what the summary of the AcceptsReturn in TextBox says:

           // Summary:
            //     Gets or sets a value that indicates how the text editing control responds when
            //     the user presses the ENTER key.
            //

    Shortly said, no words for pasting multiline text. However, there is a protected method in our control which you can override. For example:

        public class CustomTextInput : RadMaskedTextInput
        {
            protected override bool HandlePasteOverride(object value, out object returnString)
            {
                string clipboardValue = value.ToString();
                int newLinexindex = clipboardValue.IndexOfAny(new char[] { '\n', '\r' });
                if (newLinexindex != -1)
                {
                    returnString = clipboardValue.Substring(0, newLinexindex);
                }
                else
                {
                    returnString = clipboardValue;
                }
    
                return true;
            }
        }

    Other solution might be to disable our ApplicationPaste command binding in code so that the underlying TextBox works with no foreign interruption.
    However, you mentioned you want to avoid inheritance or code behind. Could you please elaborate a bit more ?

    Regards,
    Petar Mladenov
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
Back to Top