C#中简单验证输入的字符是否有效

private void IsNum(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 8)
            {
                return;
            }
            else if (e.KeyChar > '9' || e.KeyChar < '0' )
            {
                e.Handled = true;
                MessageBox.Show("无效字符");
            }
        }

    //验证电话号码

        private void txtPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
           IsNum(sender, e); 

        }

  //验证flost工资或价格

        private void txtBasePay_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar!='.')
                IsNum(sender, e);
        }

  //验证身份证号

       private void txtIDCard_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                IsNum(sender, e);
            }
            else
            {
                SendKeys.Send("{Tab}");
            }
        }

你可能感兴趣的:(C#中简单验证输入的字符是否有效)