C#ASP.NET文本框如何验证和掩盖电话号码?
我有一个文本框。
<asp:TextBox runat="server" ID="cepTelefon" CssClass="form-control cepTelefon"></asp:TextBox>
我想对此文本框进行电话号码掩盖和仅进行编号条目。我该怎么做?
我只是使用下面的验证器输入数字,但是它会产生一个问题,因为掩蔽中会有括号和空间。
<asp:RegularExpressionValidator ID="revCepPhone"
ControlToValidate="mobilePhone" runat="server"
ErrorMessage="You can only enter numbers."
ValidationExpression="\d+">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为OnKeypress事件创建自己的验证
以掩盖文本框值,您需要从用户收集输入,然后将其称为字符串,例如提交按钮等事件。
这应该是您的输出
You can create your own validate for onKeypress event
To mask a textbox value you will need to collect the input from the user then call it as a string within an event like a submit button etc.
This should be your output
好吧,如果您想要一个日期文本框或数字仅文本框或日期时间选择器?
您可以将文本框设置为这样说:
您会看到:
nofollow noreferrer“>
在您获得的时间:
此处周,您得到:
,在该列表中,有TextMode =“ Phone”。
但是,据我所知,它从未实施过。
因此,您可以设置textmode =“ number”。这将不允许任何文本或除1-9和0以外的任何其他文字(所有其他密钥压力都被忽略)。
但是,如果您想要上述花式掩蔽类型?
您真的想采用一些为您完成所有肮脏工作的库。
您可以使用一些JavaScript的Google。
另一个可能?好吧,如果您使用ajaxtoolkit,它也有一个非常不错的面具编辑器。
我不确定我是否只需要一个功能来采用整个ajaxtoolkit。但是,由于我使用的是Ajaxtool套件(它具有一个不错的对话框POP实用程序,还有一个非常不错的Ajax多个文件上加载程序。而且它还具有自动填充搜索文本框。因此,它具有许多功能
。这些功能当然是编号掩盖的
编辑
。 “ https://i.sstatic.net/zgvhl.png” alt =“在此处输入图像说明”>
,然后选择以下内容:
您现在可以设置文本框的属性 编辑。
面具
- 包括您的 /uoaxj.png“ alt =”在此处输入图像描述>
现在我们得到了:
因此,以上只能允许数字,并且会显示掩码。在您选出选项卡后,掩码确实会消失,但是有一些选项可以保留掩码(但是()()和 - 字符将成为文本框的一部分 - 您可以在保存到数据库之前将其脱离
。 上
来自桌面软件,我们总是有一个不错的掩蔽系统 - 至少在我们使用的大多数平台
。 文本模式选项非常好,看起来不错,看起来现代,您不需要任何第三方工具。
日期,数字,时间甚至日期,内置 似乎没有任何东西或工作
。
而且我当然不建议您写自己的内容,但是我想如果您可以使用JavaScript,那么您可以
说,我没有其他建议来考虑Ajaxtoolkit。您可以将包裹nuget。如前所述,它具有自动搜索文本框中的典型功能,对话框,重新ARRAGNE列表(拖动和移动),以及非常不错的Ajax Up-op-loader实用程序。
因此,如果我需要上载功能,我只能放入Ajax文件上加载器,然后得到:
它具有一个内置的进度栏,文件可以是无限的大小(在块中上载)。
因此,有很多功能。只有很大的缺点是,控件通常看起来和感觉有些过时。 (它们都应更新到漂亮的Bootstrap外观和感觉)。
但是,Web表单的AjaxtoolKit是免费的,由Devexpress的人们维护(顺便说一句,它提供了非常不错的商业工具)。
因此,花一天(或2)尝试一些jQuery和JavaScript掩盖示例。您可能会发现自己喜欢的一个。或者,考虑采用AjaxtoolKit,并使用蒙版编辑器和扩展器。我还使用了他们的HTML编辑器,而且看起来很过时,但是完成了工作。
避免在这里抱怨?开箱即用,蒙版应该是ASP.NET框架和Web表单的一部分。值得庆幸的是,这里有AjaxtoolKit,但是您可以花一些时间来搜索JavaScript蒙版编辑器。
无论您走哪条路,很明显,工具包中的一个好的解决方案是构建基于Web的软件的基本要求。
Well, if you want a date text box or number only text box, or date time picker?
You can set the textbox to say this:
And you see this:
And for date, then:
for time you get:
for week you get:
And, in that list, there is textmode="phone".
but, as far as I can tell, it never was implemented.
So, you could set textmode="number". That would NOT allow any text, or anything but 1-9 and 0 (all other key press are ignored).
However, if you going to want the above type of fancy masking?
The you REALLY want to adopt some library that done all the dirty work for you.
You can google for some JavaScript ones.
And the other possible? Well, if you been using the ajaxtoolkit, it also has a VERY nice mask editor.
I not sure I would want to adopt the WHOLE ajaxtoolkit for JUST one feature. but, since I am using the ajaxtool kit (it has a nice dialog pop utility, and a very nice ajax multiple file up-loader. And it also has autocomplte search for text box. So, it has a LOT of features.
One of the features is of course number masked edits.
So, you drop in a text box like this:
and then choose this:
You can now set properties for the text box - including your mask edit.
Say like this:
And now we get this:
so, the above will only allow numbers, and will show a mask. The mask does go away after you tab out, but there are options to KEEP the mask displayed (but then the () and - chars will be part of the text box - you could strip out before save to database.
At the end of the day?
coming from desktop software, we always had quite a nice masking system - at least on most platforms that we use.
For web based, not so much. But to be fair, I see BOATLOADS of posts on SO, and for things like date, numbers, time, and even datetime the built-in text mode options are VERY nice, look good, look modern, and you don't need ANY 3rd party tools.
Unfortantly, in that list of built in textmodes, the phone number does not seem to offer anything or work.
So, you can start google, spend a day, find some nice jQuery based masked editor, or some such. Such a mask edit utility is without question a simple basic requirement for anyone doing web development.
And I certainly do NOT suggest writing your own, but I suppose if you handy with JavaScript, then you could.
As noted, I don't have a alternative suggestion then to consider the ajaxtoolkit. You can nuget the package. And as noted, it has a boatload of typical features from auto-search text box, dialogs, re-arragne a list (drag and move), and that VERY nice ajax up-loader utility.
So if I need up-load ability, I can just drop in the ajax file up-loader, and I get this:
It has a built-in progress bar, files can be un-limited size (it up-loads in chunks).
So, there are a boatload of features. Only REALLY big downside, is that the controls often look and feel a bit outdated. (they should all be updated to nice bootstrap look and feel).
but, the ajaxtoolkit for web forms is free, maintained by the folks from devExpress (which offers very nice commercial tools by the way).
So, spend a day (or 2) trying out some jQuery and JavaScript masking examples. You might find one you like. Or, consider adopting the ajaxtoolkit, and use the masked editor and extenders they have. I also use their HTML editor, and again, it looks dated, but it gets the job done.
To avoid a complain here? Out of the box, masking should have been well part of the asp.net framework and web forms. Thankfully, there is the ajaxtoolkit, but you could spend some time googling for a JavaScript masked editor.
No matter which road you take, it rather obvious that a good solution in your tool bag is a basic requirement for building web based software.