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

MaskedTextInput: Prevent entering text with newlines

2 Answers 268 Views
MaskedInput (Numeric, DateTime, Text, Currency)
This is a migrated thread and some comments may be shown as answers.
bitbonk
Top achievements
Rank 1
bitbonk asked on 30 Jun 2020, 08:47 AM

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 Answers, 1 is accepted

Sort by
0
bitbonk
Top achievements
Rank 1
answered on 30 Jun 2020, 08:56 AM

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.

0
Petar Mladenov
Telerik team
answered on 02 Jul 2020, 11:47 AM

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.
Tags
MaskedInput (Numeric, DateTime, Text, Currency)
Asked by
bitbonk
Top achievements
Rank 1
Answers by
bitbonk
Top achievements
Rank 1
Petar Mladenov
Telerik team
Share this question
or