2
您好,我有一個JavaScript代碼的按鍵,我想放在「。」。和文本框中的數字。我可以輸入數字,但不能輸入「。」。我有Javascript和HTMl以及小提琴鏈接。任何人都可以幫助我嗎?放置正則表達式「。」在文本框的按鍵
<script>
var aa_zChar = new Array(' ');
var aa_maxphonelength = 8;
var aa_phonevalue1;
var aa_phonevalue2;
var aa_cursorposition;
function aa_ParseForNumber1(object){
aa_phonevalue1 = aa_ParseChar(object.value, aa_zChar);
}
function a_ParseForNumber2(object){
aa_phonevalue2 = aa_ParseChar(object.value, aa_zChar);
}
function aa_up(object,e) {
if(e){
e = e
} else {
e = window.event
}
if(e.which){
var keycode = e.which
} else {
var keycode = e.keyCode
}
aa_ParseForNumber1(object)
aa_ValidatePhone(object)
}
function aa_down(object,e) {
if(e){
e = e
} else {
e = window.event
}
if(e.which){
var keycode = e.which
} else {
var keycode = e.keyCode
}
aa_ParseForNumber2(object)
}
function aa_ValidatePhone(object){
var p = aa_phonevalue1
p = p.replace(/[^\d]*/gi,"")
object.value=p
}
function aa_ParseChar(sStr, sChar)
{
if (sChar.length == null)
{
aa_zChar = new Array(sChar);
}
else aa_zChar = sChar;
for (i=0; i<aa_zChar.length; i++)
{
sNewStr = "";
var iStart = 0;
var iEnd = sStr.indexOf(sChar[i]);
while (iEnd != -1)
{
sNewStr += sStr.substring(iStart, iEnd);
iStart = iEnd + 1;
iEnd = sStr.indexOf(sChar[i], iStart);
}
sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);
sStr = sNewStr;
}
return sNewStr;
}
</script>
HTML
<input name="name" type="text" maxlength="100" id="name" value="" onKeyDown="aa_down(this,event);" onKeyUp="aa_up(this,event);" />
一個盲目的答案是將'/ [^ \ d] */gi'改成'/ [^ \ d。] */gi'(我在最後但實際上是多餘的)。但是,代碼似乎在電話號碼中讀取? – nhahtdh
不是電話號碼格式。我其實並沒有改變這些變數。這是用於金額 – Soumya