我现在做一个数据插入,我其中的数据有Decimal类型  当在界面中的TextBox中不输入值时,
1.就会出现错误:输入字符串的格式不正确!请问怎么处理!
2.在TextBox中 只允许输入数字, 或者判断出是否包含其它字符
请各位前辈指点!小弟感激不尽!

解决方案 »

  1.   

    try
    {
       decimal a = decimal.Parse(this.TextBox1.Text);
    }
    catch
    {
       ...; //输入格式错误,请重新输入
       return;
    }
      

  2.   

    用正则表达式做判断,非数字就不能通过,下面的不够完善,再完善
    <script language="javascript">
    function isNumber(str)
    {
    return/(^-?|^\+?|\d)\d+$/.test(str) || /(^-?|^\+?|^\d?)\d*\.\d+$/.test(str);
    }
    function test()
    {
    if(isNumber(document.getElementById("TextBox1").value)==false)
    {
    alert("不是数字");
    return;
    }
    }
    </script><form id="Form1" method="post" runat="server">
    <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 256px; POSITION: absolute; TOP: 80px" runat="server"></asp:TextBox>
    <INPUT onclick="test();" style="Z-INDEX: 102; LEFT: 432px; POSITION: absolute; TOP: 88px" type="button" value="Button">
    </form>
      

  3.   

    各位前辈我的程序时在WinForm中的!
      

  4.   

    这个跟asp.net和winform没有什么区别,
    Convert.ToDecimal 方法,.net1.1 SDK,
    ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfSystemConvertClassToDecimalTopic.htm
      

  5.   

    可是当我不在TextBox中写值的话 转换会出现错误的

      

  6.   

    //设置只能输入数字,同时将textBox1的ImeMode设置Disable;
    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if(e.KeyChar<48)
    {
    e.Handled=true;
    }
    else if(e.KeyChar>57)
    {
    e.Handled=true;
    }
    }
    //验证
    private void button1_Click(object sender, System.EventArgs e)
    {
    if(textBox1.Text=="")
    {
    MessageBox.Show ("情输入!","错误",MessageBoxButtons.OK,MessageBoxIcon.Information););
    }
    else
    {
    int a = textBox1.Text.Trim();
    你的进行数据库操作;
    }
    }