Revision: 12401
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at March 12, 2009 17:31 by blackf0rk
Initial Code
// Limit the text field to only numbers (with decimals) function format(input){ var num = input.value.replace(/\,/g,''); if(!isNaN(num)){ if(num.indexOf('.') > -1){ num = num.split('.'); num[0] = num[0].toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,''); if(num[1].length > 2){ alert('You may only enter two decimals!'); num[1] = num[1].substring(0,num[1].length-1); } input.value = num[0]+'.'+num[1]; } else { input.value = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'') }; } else { alert('You may enter only numbers in this field!'); input.value = input.value.substring(0,input.value.length-1); } } // Limit the text field to only numbers (no decimals) function formatInt(input){ var num = input.value.replace(/\,/g,''); if(!isNaN(num)){ if(num.indexOf('.') > -1) { alert("You may not enter any decimals."); input.value = input.value.substring(0,input.value.length-1); } } else { alert('You may enter only numbers in this field!'); input.value = input.value.substring(0,input.value.length-1); } } //utilization <input id="text" name="text" size="5" onkeyup="format(this);">
Initial URL
http://codefinds.blogspot.com/2007/10/limit-text-input-to-numbers-javascript.html
Initial Description
Initial Title
Limit text input to numbers (with or without decimals)
Initial Tags
javascript
Initial Language
JavaScript