如何在文本中间编辑文本字段

时间:2012-08-03 08:04:52

标签: javascript

//The porpost of this function is prevent user type any thing intext field except 0-9 and .

//For recieve value,event and keycode from text field.    
    function allowOnlyFloatingPointNumbers(textbox,val,key)
        {
        var MaxLen = 11;
        var val2 = val.replace(/,/g,"");
        var letterNumber = /^[0-9.]+$/;
         if((val2.match(letterNumber)))
          {
                textbox.value = val;
          }
        else
          {
                textbox.value = val.substr(0,val.length-1);
                return;
          }

            var valtmp = val;
            if(valtmp.indexOf(".")<0)
            {
                if(valtmp.replace(/,/g,"").length>MaxLen)
                {
                    textbox.value = addCommas(val.substr(0,val.length-1));
                }
                else
                {
                    textbox.value = addCommas(valtmp.replace(/,/g,""));
                }
            }
            else
            {
                val = val.replace(/[^0-9.]/g, "");         // strip non-digit chars
                textbox.value = addCommas(val);            // replace textbox value
                var indx = val.indexOf(".");
                if(indx>-1)
                {
                    var lng = val.substr(indx+1).length;
                    if(lng>2)
                    {
                        textbox.value = addCommas(val.substr(0,val.length-1));
                    }
                }
            }

        }
    function addCommas(nStr)
                {
                        nStr += "";
                        x = nStr.split(".");
                        x1 = x[0];
                        x2 = x.length > 1 ? "." + x[1] : "";
                        var rgx = /(\d+)(\d{3})/;
                        while (rgx.test(x1)) {
                            x1 = x1.replace(rgx, "$1" + "," + "$2");
                        }
                        return x1 + x2;
                }

现在,如果我键入987654321,它将是987,654,321in textbox

但如果我需要在文本中间编辑,如更改5到0 文本末尾插入文本为987,643,210 我需要它是987,604,321我应该改变的部分功能。

我使用IE 9。

以下是示例http://jsbin.com/arivug/1

0 个答案:

没有答案