比如输入1234556,自动变成1,234,556,实在输入过程中自动变,不是在焦点转移的时候变
比如1234,自动变成1,234
继续输入12345,自动变成12,345

解决方案 »

  1.   


    <html>
    <body>
    <input id="test" onblur="asfmanFormat(this);" />
    <script>
    function asfmanFormat(n) 

      var o=n;
      n=n.value.split(".");
    var str="";  
    for(var i=0,l=n.length;i<l;i++)

      if(i==1)
      str += "." + n[i].replace(/,/g,"").split("").reverse().join("").replace(/(.{3})/g,"$1,").replace(/,$/,"").split("").reverse().join(""); 
      else
      str += n[i].replace(/,/g,"").split("").reverse().join("").replace(/(.{3})/g,"$1,").replace(/,$/,"").split("").reverse().join(""); 
    }
    o.value = str
    return str;

    </script>
    </body>
    </html>
      

  2.   

    我可能没说清楚,我指的是winform应用程序
      

  3.   


    //只能输入数字
    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        string inputText = textBox1.Text.Replace(",", "") + e.KeyChar;
        e.Handled = e.KeyChar == (char)8 ? false : !Regex.IsMatch(inputText, @"^[1-9]\d*$");
    }
    //格式化输入
    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        string inputText = textBox1.Text.Replace(",", "");
        if (inputText.Length > 3)
        {
             textBox1.Text = double.Parse(inputText).ToString("0,000");
             textBox1.SelectionStart = textBox1.Text.Length;
        }
    }
      

  4.   

    核心代码就是在textbox输入框改变时候
    double text = double.Parse(test.Text);
    test.Text = String.Format("{0:N2}", text);但是要做很多判断 楼主自己写下吧
      

  5.   

    不合理的UI设计,会造成操作人员混淆。
    看下MaskedTextBox:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.aspx
      

  6.   

    看这个如何:
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                int mL = textBox1.SelectionStart;
                string oldStr = textBox1.Text;
                if (oldStr == null || oldStr.Length == 0)
                    return;
                double text = double.Parse(textBox1.Text);
                string newStr = String.Format("{0:N0}", text);
                textBox1.Text = newStr;
                textBox1.SelectionStart = mL + newStr.Length - oldStr.Length;
            }
      

  7.   


    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        try
        {
            string inputText = textBox1.Text.Replace(",", "");
            if (inputText.Length > 3)
            {
                textBox1.Text = double.Parse(inputText).ToString("0,000");
                textBox1.SelectionStart = textBox1.Text.Length;
            }
            else
            {
                if (inputText != "")
                {
                    textBox1.Text = double.Parse(inputText).ToString("0");
                    textBox1.SelectionStart = textBox1.Text.Length;
                }        }
        }
        catch
        {
            MessageBox.Show("格式不正确");
            textBox1.Text = "";
        }}