C# 屏蔽电话号码的文本框视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18258709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:42:28  来源:igfitidea点击:

Mask Textbox view for a phone number

c#asp.net.netajaxcontroltoolkit

提问by Madam Zu Zu

i am trying to format a text box so that when the users can type in a phone number and it would automatically put it in the following format :(999)999-9999

我正在尝试格式化文本框,以便当用户可以输入电话号码时,它会自动将其放入以下格式:(999)999-9999

i tried using the MaskedEditExtender, it works but it does the complete opposite of what i need.

我尝试使用 MaskedEditExtender,它可以工作,但它与我需要的完全相反。

when i click on the field it shows the correct format, but once i mouse out it still only shows 9999999999

当我点击该字段时,它会显示正确的格式,但是一旦我将鼠标移开,它仍然只显示 9999999999

how can i do it so that the user ONLY types in numbers and it puts dashes and parentheses where it's supposed to automatically(which it does now), but keep the format when i click out of it.

我该怎么做才能让用户只输入数字并将破折号和括号放在它应该自动的位置(现在这样做),但是当我点击它时保持格式。

<asp:TextBox ID="txtSuperPhone" runat="server" CssClass="myinputstyle"  />
    <asp:MaskedEditExtender ID="mskSuperPhone" runat="server" TargetControlID="txtSuperPhone" Mask="(999)999-9999"/>

采纳答案by Smeegs

To answer your question, you have to set the "ClearMaskOnLostFocus" to false on the extender for the mask to show all the time.

要回答您的问题,您必须在扩展器上将“ClearMaskOnLostFocus”设置为 false 以使掩码始终显示。

That being said....

话虽如此....

I strongly suggest that you use the jquery alternative.

我强烈建议您使用 jquery 替代方案。

http://digitalbush.com/projects/masked-input-plugin/

http://digitalbush.com/projects/masked-input-plugin/

The reason I say this is because the maskededitextender does not work in safari.

我这么说的原因是因为 maskededitextender 在 safari 中不起作用。

回答by Derdlim

Try this

尝试这个

The propoerty "ClearMaskOnLostFocus" must be false.

属性“ClearMaskOnLostFocus”必须为假。

<asp:MaskedEditExtender ID="mskSuperPhone" runat="server"
                   TargetControlID="txtSuperPhone"
                   ClearMaskOnLostFocus ="false"
                   MaskType="None"
                   Mask="(999)999-9999" 
                   MessageValidatorTip="true"
                   InputDirection="LeftToRight"
                   ErrorTooltipEnabled="True">
  </asp:MaskedEditExtender>