PDA

View Full Version : Form field Limiter IE NS6!



jscheuer1
03-04-2005, 06:11 AM
This script was said to be good in IE5+ and NS6+, however . . . It choked in NS7.2, I have an updated version that does work in both NS7.2 and IE6 only two lines are changed (one changed, one removed, indicated in comments), so here it is:


<script type="text/javascript">
<!--
/*
Form field Limiter script- By Dynamic Drive
For full source code and more DHTML scripts, visit http://www.dynamicdrive.com
This credit MUST stay intact for use
*/

var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder){
if (window.event&&event.srcElement.value.length>=maxlength)
return false
else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
var pressedkey=["[a-z]", "[A-Z]", "[0-9]", ".", ",", "/"] //detect alphanumeric keys (this line changed)
//if (pressedkey.test(String.fromCharCode(e.which))) //(this line removed)
e.stopPropagation()
}
}

function countlimit(maxlength,e,placeholder){
var theform=eval(placeholder)
var lengthleft=maxlength-theform.value.length
var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
if (window.event||e.target&&e.target==eval(placeholder)){
if (lengthleft<0)
theform.value=theform.value.substring(0,maxlength)
placeholderobj.innerHTML=lengthleft
}
}


function displaylimit(thename, theid, thelimit){
var theform=theid!=""? document.getElementById(theid) : thename
var limit_text='<b><span id="'+theform.toString()+'">'+thelimit+'</span></b> characters remaining on your input limit'
if (document.all||ns6)
document.write(limit_text)
if (document.all){
eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
}
else if (ns6){
document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true);
document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true);
}
}
//-->
</script>

ddadmin
03-16-2005, 06:18 AM
Hi:
Thanks for the code. Can you be more specific on what the problem was in NS7.2? Just tried testing the original script in that browser, and all seemed well.

jscheuer1
03-16-2005, 09:21 AM
I'm trying to get it to repeat by restoring the code from the DD website. Won't happen. What happened the first time was the script would not work, gave an error on line:

var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys I changed the line as I said in my first post on this and then got the error for the line I commented out. Now, however, putting it all back as it is in your version, it is working fine, no errors. I'm not certain but looking at how I rewrote the 'offending' line it looks as if the initial '/' of the re was missing when I first tested it. It could have gotten stripped out when I was pasting it into a remote PHP template. Then I saved the source generated from the template to debug it. Sorry about that. Wouldn't be the first time a '/' got stripped in just such a fashion, then again it might, as I don't recall being aware of that possibility at that time. I learned it later while pasting simple links into the template.