Reputation: 23
Hi there i have this script and im trying to make it work so that only a certain amount of numbers (8 numbers) are allowed in it. What do i have to add to this??
<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>
Upvotes: 1
Views: 79
Reputation: 2966
You can either add the attribute size (size="8") to your field directly or do it using JS :
var sizeLimitation = document.createAttribute('size');
sizeLimitation.value = 8;
myfield.setAttributeNode(sizeLimitation);
Upvotes: 1