Need a way to limit what gets entered into an HTML textbox?
Plug this script into your textbox using…
onKeyPress="javascript:return limitinput(event, '0123456789', true);"
Variables
- event – The event that is passed. Just leave is as ‘event’ when you call it.
- strList – Either a black list or white list depending on the next variable.
- bAllow – If false strList acts as a black list. If true strList acts as a white list.
function limitinput(evt, strList, bAllow) /*Limits the input to strList. If bAllow is true, then only allow what is in strList. If bAllow is false, then do not allow what is in strList. http://blog.nutt.net */ { var charCode = evt.keyCode; if (charCode==0) { charCode = evt.which; } var strChar = String.fromCharCode(charCode); /*controlArray holds the ASCII codes for valid control commands (BS, CR, LF, etc)*/ var controlArray = Array(0, 8, 9, 10, 13, 27); var intOut = 0; if (bAllow==true) { if (charCode==8 || charCode==9 || charCode==37 || charCode==39 || charCode==46 || charCode==116 || (strList.indexOf(strChar)!=-1)) /*Valid*/ { return true; } else { return false; } } else { if (charCode==8 || charCode==9 || charCode==37 || charCode==39 | charCode==46 || charCode==116 || (strList.indexOf(strChar)==-1)) { return true; } else { return false; } } }
Be First to Comment