防止文本框输入字符串 [英] Preventing textbox to enter string

查看:77
本文介绍了防止文本框输入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一个文本框,该文本框接受大于10的手机号码.问题在于它也接受字符串.我可以验证它是否仅接受数字.这是我的代码:

i am trying a textbox which accept mobile number if it is greater than ten. Problem is that it also accept the string. can i validate it to accept only digit. here is my code:

<script language="C#" runat="server">
    protected void CustomValidatorPhone(object source, ServerValidateEventArgs args)
  {
      if (txtMobileNumber.Text.Length <=10)
    {  args.IsValid = true; }
   else
    {  args.IsValid = false; }
}</script>


然后调用它:


and callling it on:

<asp:CustomValidator CSSClass="body" id="cusPhone" ValidationGroup="EditProfile"   OnServerValidate="CustomValidatorPhone" Text="Invalid mobile Number"   runat="server" /> </div>


所以在脚本中我需要添加或扩展我的代码..预先感谢..


so in script what i need to add or extend my code.. thanks in advance..

推荐答案

您只需将 MaxLength = 10 赋予文本框txtMobileNumber.

您可以使用 RegularExpression [ javascript [
You just give MaxLength=10 to the textbox txtMobileNumber.

You can use RegularExpression[^] or javascript[^]

Do customizations based on your need, that''s all.


为此,您可以使用仅包含数字的正则表达式.
有关详细信息,请
看看
For that, you can use regular expressions, that takes onl y numbers.
For details, Have a look


您可以使用RegularExpression查找文本是否为数字.
下面是代码

You can use RegularExpression to find whether the text is number.
Below is the code

System.Text.RegularExpressions.Regex regularExpression = new System.Text.RegularExpressions.Regex("^-[0-9]+


这篇关于防止文本框输入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆