判断TextBox输入的内容是否含有字母,若含有其他内容则抛出自定义异常



判断TextBox输入的内容是否含有数字:

法一:

//只能判断是否含有字母

 foreach (char c in TextBox1.Text)
{
        if (char.IsLetter(c))
        {
            //有字母
            throw new UserDefinedException("输入的内容中含有字母");
        }
}

法二:

//可以通过改变判断的asc码制,想检查是否含有什么字符都可以

string texts = TextBox1.Text;

Label2.text=text[1].tostring()+texts[2].tostring();//string在内部是以字符数组存储的,我们可以通过下标来调用
for (int i = 0; i < TextBox1.Text.Length; i++)
{
    int ascNumber = Convert.ToChar(texts[i].ToString());
    if ((Convert.ToChar(texts[i].ToString()) > 64 && Convert.ToChar(texts[i].ToString()) > 91) ||

        (Convert.ToChar(texts[i].ToString()) > 96 && Convert.ToChar(texts[i].ToString()) > 123))
    {
        throw new UserDefinedException("输入的内容中含有字母");
    }
}

在asc码表中:0-9对应的asc码值是48-57   A-Z对应的asc码值是65-90   a-z对应的asc码值是97-122


ASP.NET C#检查TextBox输入的内容是否含有字母,若含有其他内容则抛出自定义异常原代码如下:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (char c in TextBox1.Text)
            {
                if (char.IsLetter(c))
                {
                    //有字母
                    throw new UserDefinedException("输入的内容中含有字母");
                }
            }
            //string texts = TextBox1.Text;
            //for (int i = 0; i < TextBox1.Text.Length; i++)
            //{
            //    int ascNumber = Convert.ToChar(texts[i].ToString());
            //    if ((Convert.ToChar(texts[i].ToString()) > 64 && Convert.ToChar(texts[i].ToString()) > 91) || (Convert.ToChar(texts[i].ToString()) > 96 && Convert.ToChar(texts[i].ToString()) > 123))
            //    {
            //        throw new UserDefinedException("输入的内容中含有字母");
            //    }
            //}
            int number = int.Parse(TextBox1.Text);
            if (number > -1 && number < 11)
            {
                Label2.Text = "输入的值为:" + number;
            }
            else
            {
                throw new UserDefinedException("输入的数字超出范围");
            }
            Label2.Text = "(抛出异常之后程序继续执行)输入的值为:" + TextBox1.Text;
        }
        catch (UserDefinedException ude)
        {
            Response.Write("<script>alert('"+ude.Message+"')</script>");
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('捕获系统异常!')</script>");
        }
    }
}
public class UserDefinedException : ApplicationException
{
 //public MyException(){} 
         public UserDefinedException(string message) : base(message) { } 
  
         public override string Message 
         { 
             get 
             {
                 string result="问题:["+base.Message+"]已经过处理";
                 return result;
                 //return base.Message; 
             } 
         } 
}


如果想多个页面都调用自定义抛出异常类的话最好是在App_Code中建个类文件,专门解决异常问题。

类文件中原代码如下(类文件名称为UserDefinedException.cs):

/// <summary>
/// UserDefinedException 的摘要说明
/// </summary>
public class UserDefinedException : ApplicationException
{
    //public MyException(){} 
    public UserDefinedException(string message) : base(message) { }

    public override string Message
    {
        get
        {
            string result = "问题:[" + base.Message + "]已经过处理";
            return result;
            //return base.Message; 
        }
    }
}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

changuncle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值