Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: ilosc znaków
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
bronx
witam

jak ma wyglądać skrypt który pokazuje ile jeszcze znaków można wpisać w odpowiedni formularz questionmark.gif

z góry dzieki za pomoc

pozdrawiam
wassago
  1. <!--
  2. This file retrieved from the JS-Examples archives
  3. 1000s of free ready to use scripts, tutorials, forums.
  4. Author: Saqib - http://usgems.net/HALF
  5. -->
  6.  
  7. function alll() {
  8. var m = document.f1.t1.value;
  9. var mm = m.length;
  10. document.f1.t2.value = mm;
  11. if(mm >= 10) {
  12. alert("Max is 10");
  13. document.f1.t1.value = "";
  14. }
  15. setTimeout("alll()" ,0);
  16. }
  17.  
  18.  
  19. </head>
  20.  
  21. <form name="f1">
  22. <input type="Textbox" name="t1" onfocus="alll()"><br />
  23. <input type="Textbox" name="t2">
  24. </form>
  25. </body>
  26. </html>
bronx
dzięki, ale chodziło mi o taki który pokazuje np. 30 i z każdym wpisanym znakiem ta liczba maleje smile.gif
wassago
nie mam czasu szukac wiec: google.pl => "javascript+count+letters",
powodzenia winksmiley.jpg
czachor
Kod
function zliczaj_znaki() {
    if(document.form.pole.value.length<501){
    a=document.form.pole.value.length;
    b=500;
    c=b-a;
    document.form.pole_z_odliczaniem.value=c;
    }
    else{
    alert('Przekroczono dozwoloną ilość znaków exclamation.gif!');
}}

pole - z tego zliczasz znaki
pole_z_odliczaniem - tu następuje odliczanie (ustaw value="500")
miedzna
Cytat(bronx @ 2004-07-03 17:21:14)
witam

jak ma wyglądać skrypt który pokazuje ile jeszcze znaków można wpisać w odpowiedni formularz questionmark.gif

z góry dzieki za pomoc

pozdrawiam

Tu scrypt:

  1. <script type="text/javascript">
  2. var ns6=document.getElementById&&!document.all
  3. function restrictinput(maxlength,e,placeholder){
  4. if (window.event&&event.srcElement.value.length>=maxlength)
  5. return false
  6. else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
  7. var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
  8. if (pressedkey.test(String.fromCharCode(e.which)))
  9. e.stopPropagation()
  10. }
  11. }
  12. function countlimit(maxlength,e,placeholder){
  13. var theform=eval(placeholder)
  14. var lengthleft=maxlength-theform.value.length
  15. var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
  16. if (window.event||e.target&&e.target==eval(placeholder)){
  17. if (lengthleft<0)
  18. theform.value=theform.value.substring(0,maxlength)
  19. placeholderobj.innerHTML=lengthleft
  20. }
  21. }
  22. function displaylimit(thename, theid, thelimit){
  23. var theform=theid!=""? document.getElementById(theid) : thename
  24. var limit_text='<font color="red" size="2"><b><span id="'+theform.toString()+'">'+thelimit+'</span></b></font> znaków do wprowadzenia.'
  25. if (document.all||ns6)
  26. document.write(limit_text)
  27. if (document.all){
  28. eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
  29. eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
  30. }
  31. else if (ns6){
  32. document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true);
  33. document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true);
  34. }
  35. }



W form dajesz name="sampleform"
<form action="dodaj.php" method="post" name="sampleform">

a przy inpucie:
<input type="text" name="tytul">
<script>
displaylimit("document.sampleform.tytul","",60)
</script>

calosc wyglądała by tak:

  1. <script type="text/javascript">
  2. var ns6=document.getElementById&&!document.all
  3. function restrictinput(maxlength,e,placeholder){
  4. if (window.event&&event.srcElement.value.length>=maxlength)
  5. return false
  6. else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
  7. var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
  8. if (pressedkey.test(String.fromCharCode(e.which)))
  9. e.stopPropagation()
  10. }
  11. }
  12. function countlimit(maxlength,e,placeholder){
  13. var theform=eval(placeholder)
  14. var lengthleft=maxlength-theform.value.length
  15. var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder)
  16. if (window.event||e.target&&e.target==eval(placeholder)){
  17. if (lengthleft<0)
  18. theform.value=theform.value.substring(0,maxlength)
  19. placeholderobj.innerHTML=lengthleft
  20. }
  21. }
  22. function displaylimit(thename, theid, thelimit){
  23. var theform=theid!=""? document.getElementById(theid) : thename
  24. var limit_text='<font color="red" size="2"><b><span id="'+theform.toString()+'">'+thelimit+'</span></b></font> znaków do wprowadzenia.'
  25. if (document.all||ns6)
  26. document.write(limit_text)
  27. if (document.all){
  28. eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
  29. eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform)}
  30. }
  31. else if (ns6){
  32. document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true);
  33. document.body.addEventListener('keyup', function(event) { countlimit(thelimit,event,theform) }, true);
  34. }
  35. }
  36.  
  37.  
  38.  
  39. <form action="dodaj.php" method="post" name="sampleform">
  40. <B>Tytuł</B> (max 60 znaków)<BR>
  41. <input type="text" name="tytul" maxlength="60">
  42. displaylimit("document.sampleform.tytul","",60)
  43. <BR><BR>
  44. <B>Wstęp</B> (max 200 znaków)<BR>
  45. <textarea cols="60" rows="5" name="wstep"></textarea>
  46. displaylimit("","wstep",200)
  47. <input type="submit" name="submit" value="Wy&para;lij" class="ramka" style="margin-top:5px;">
  48. </form>
bronx
ooooooooo wielkie dzięki, zaje*** skrypt smile.gif właśnie o to mi chodziło

THX i pozdrawiam smile.gif
Vir
A ja napisałem i podzielę się z Wami takim oto skryptem:

java script:
Kod
function count(mCh,p,w)
   {
     t=document.getElementById(w);s=document.getElementById(p);s.innerHTML=mCh;
     if(t.value.length>mCh){alert('Za duża liczba znaków!');t.value=t.value.substring(0,mCh)}
     i=mCh-t.value.length;s.innerHTML="Zostało "+i+" znaków do wpisania.";
   }


HTML:
  1. <textarea cols="5" rows="2" name="test" id="txt1" onkeyup="count(180,'char1','txt1')"></textarea>
  2. <br />
  3. <span id='char1'></span>
  4. <script type="text/javascript">count(180,'char1','txt1');</script>


Kod
count(mCh,p,w)

gdzie:
mCh - maksymalna liczba znaków, które mogą być wpisane
p - pole, w którym ma być wyświetlana ta liczba (mCh)
w - pole (textarea, text), do którego odnosi się licznik

Podstawowe zalety:
- możliwość użycia w odniesieniu do kilku pól
- jeżeli przekroczysz dozwoloną liczbę znaków to nie tylko pojawi się alert, ale także znaki powyżej określonego limitu zostaną obcięte
ridle
  1.  
  2. <div class="full clearfix border_t box">
  3. <p>
  4. <label for="comment"><?php echo SPEC($GLOBALS['_LANG']['_tpl_add14']) ?><span class="required">*</span></label><br />
  5. <textarea tabindex="4" class="long" rows="4" name="form[description]" id="short2" style="height:300px;"><?php if(isset($_POST['action']) && !isset($data) ){ echo $_POST['form']['description']; }elseif(isset($data)){ echo $data['post_content']; } ?></textarea><br />
  6. <small><?php echo SPEC($GLOBALS['_LANG']['_tpl_add15']) ?></small>
  7. </p>
  8. </div>


A jak wstawić licznik zanków w ten kod ponieważ meczę się już godzinę i nie mogę ogarnąć. Pomóżcie proszę bo narazie raczkuję w PHP i Java.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.