嗨,我有这个脚本,我试图让它工作,以便只允许一定数量的数字(8个数字)。我需要添加什么?
<SCRIPT TYPE="text/javascript">
function numbersonly(myfield, e, dec)
{
var key;
var keychar;
if (window.event)
key = window.event.keyCode;
else if (e)
key = e.which;
else
return true;
keychar = String.fromCharCode(key);
// control keys
if ((key==null) || (key==0) || (key==8) ||
(key==9) || (key==13) || (key==27) )
return true;
// numbers
else if ((("0123456789").indexOf(keychar) > -1))
return true;
// decimal point jump
else if (dec && (keychar == "."))
{
myfield.form.elements[dec].focus();
return false;
}
else
return false;
}
</SCRIPT>
答案 0 :(得分:1)
您可以直接将字段 size (size =“8”)添加到字段中,也可以使用JS执行:
var sizeLimitation = document.createAttribute('size');
sizeLimitation.value = 8;
myfield.setAttributeNode(sizeLimitation);