JavaScript Schnipsel

Zeichen zaehlen

Mit diesem Script lassen sich die in ein Textfeld eingegebenen zeichen zählen. Die ausgabe kann an einer belibigen stelle erfolgen. Die ausgabe wird als Text ausgegeben nicht wie bei vielen anderen in einem Textfeld.

Drucken
  1.  <html  
  2.     
  3.  <head  
  4.  <script  
  5.  /*   
  6.  Form field Limiter script- By Dynamic Drive   
  7.  For full source code and more DHTML scripts,   
  8.  visit http://www.dynamicdrive.com   
  9.  This credit MUST stay intact for use   
  10.  */   
  11.     
  12.  var ns6=document.getElementById&&!document.all   
  13.     
  14.  function restrictinput(maxlength,e,placeholder){   
  15.  if (window.event&&   
  16.  event.srcElement.value.length>=maxlength  
  17.  return false   
  18.  else if (e.target&&   
  19.  e.target==eval(placeholder)&&   
  20.  e.target.value.length>=maxlength){   
  21.  var pressedkey=/[a-zA-Z0-9.,/]/   
  22.  if(pressedkey.test  
  23.  String.fromCharCode(e.which)))   
  24.  e.stopPropagation()   
  25.    
  26.    
  27.     
  28.  function countlimit(maxlength,e,placeholder){   
  29.  var theform=eval(placeholder  
  30.  var lengthleft=maxlength-theform.value.length   
  31.  var placeholderobj=document.all  
  32.  document.all[placeholder] :   
  33.  document.getElementById(placeholder  
  34.  if (window.event||e.target&&   
  35.  e.target==eval(placeholder)){   
  36.  if (lengthleft<0  
  37.  theform.value  
  38.  theform.value.substring(0,maxlength  
  39.  placeholderobj.innerHTML=lengthleft   
  40.    
  41.    
  42.     
  43.     
  44.  function displaylimit(theform,thelimit){   
  45.  var limit_text='noch <b><span id="'   
  46.  +theform.toString()+'">'+thelimit  
  47.  </span></bZeichen  
  48.  if (document.all||ns6  
  49.  document.write(limit_text  
  50.  if (document.all){   
  51.  eval(theform).onkeypress=function(){   
  52.  return   
  53.  restrictinput(thelimit,event,theform)}   
  54.  eval(theform).onkeyup=function(){   
  55.  countlimit(thelimit,event,theform)}   
  56.    
  57.  else if (ns6){   
  58.  document.body.addEventListener('keypress'  
  59.  function(event) {    
  60.  restrictinput(thelimit,event,theform)},true);    
  61.  document.body.addEventListener('keyup'  
  62.  function(event) {   
  63.  countlimit(thelimit,event,theform)},true);    
  64.    
  65.    
  66.  </script>   
  67.  </head  
  68.     
  69.  <body  
  70.  <form name="sampleform"  
  71.  <input type="text" name="n1" size="20"  
  72.  <br  
  73.  <script  
  74.  displaylimit("document.sampleform.n1",5  
  75.  </script>   
  76.  <p  
  77.  <textarea name="n2" cols="15" rows="5"  
  78.  </textarea><br  
  79.  <script  
  80.  displaylimit("document.sampleform.n2",10  
  81.  </script>   
  82.     
  83.  </form  
  84.  </body  
  85.     
  86.  </html  






T: 34979 G: 7 H: 10 T: +4 O: 5