Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Sprawdzanie zaznaczenia pola "radio", validacja formularza
Mion
post
Post #1





Grupa: Zarejestrowani
Postów: 105
Pomógł: 6
Dołączył: 7.03.2005

Ostrzeżenie: (0%)
-----


Mam problem z validacja formularza zawierającego pola opcjonalnego wyboru typ "radio". Do validacji formularza stosuje zamieszczony poniżej kod js który działa jak jest tylko jedno pole radio. Niestety jak dochodzi opcjonalnie - niestety przestaje działać tj.. Nie pojawia się alert i można wysłać z NIEzaznaczonym jednym z pól wyboru.... Proszę o pomoc ..
Kod
<script type="text/javascript">
function formCheck(form) {
if (form.pojazd.checked==false)
{alert("Jaki pojazd ?");return false;}
}
</script>

formularz:
Kod
<form method="post"  onsubmit="return formCheck(this)">
<input type="radio" name="pojazd" value="1"> Osobowy<br>
<input type="radio" name="pojazd" value="2"> Dostawczy<br>
<input type="submit" value="Submit">


Nikt się nie czuje na siłach w pomocy z tym problemem? (IMG:http://forum.php.pl/style_emoticons/default/sad.gif)

Ten post edytował Mion 7.01.2006, 13:49:08
Go to the top of the page
+Quote Post
2 Stron V   1 2 >  
Start new topic
Odpowiedzi (1 - 19)
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




nadaj dla kazdego z radio inne id, odwoluj sie do radio poprzez document.getElementById('idradio') i dla kazdego radio sprawdzaj cze jest zaznaczony. jesli zaden nie jest zaznaczony, to wtedy wywalaj alerta
Go to the top of the page
+Quote Post
Mion
post
Post #3





Grupa: Zarejestrowani
Postów: 105
Pomógł: 6
Dołączył: 7.03.2005

Ostrzeżenie: (0%)
-----


A w kodzie jak by to wyglądało. Niestety nie mam doświadczenia w DOM (IMG:http://forum.php.pl/style_emoticons/default/sad.gif)
Go to the top of the page
+Quote Post
nospor
post
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




  1. <form method="post" onsubmit="return formCheck();">
  2. <input type="radio" name="pojazd" value="1" id="p1"> Osobowy<br>
  3. <input type="radio" name="pojazd" value="2" id="p2"> Dostawczy<br>
  4. <input type="submit" value="Submit">


Kod
<script type="text/javascript">
function formCheck() {
if (document.getElementById('p1').checked==false && document.getElementById('p2').checked==false)
{alert("Jaki pojazd ?");return false;}
return true;
}
</script>
Go to the top of the page
+Quote Post
Mion
post
Post #5





Grupa: Zarejestrowani
Postów: 105
Pomógł: 6
Dołączył: 7.03.2005

Ostrzeżenie: (0%)
-----


Wielkie dzwięki ! O to właśnie chodziło (IMG:http://forum.php.pl/style_emoticons/default/thumbsupsmileyanim.gif)
Go to the top of the page
+Quote Post
piwib
post
Post #6





Grupa: Zarejestrowani
Postów: 51
Pomógł: 0
Dołączył: 7.11.2005

Ostrzeżenie: (0%)
-----


A jak byś to zrobił z polem tekstowym type=text (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)
Go to the top of the page
+Quote Post
Mion
post
Post #7





Grupa: Zarejestrowani
Postów: 105
Pomógł: 6
Dołączył: 7.03.2005

Ostrzeżenie: (0%)
-----


Cytat(piwib @ 2006-02-05 22:34:40)
A jak byś to zrobił z polem tekstowym type=text (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

Tak jak podał Kolega nospor jest wyśmienicie, nie potrzebuję już innych kombinacji (IMG:http://forum.php.pl/style_emoticons/default/cool.gif)
Go to the top of the page
+Quote Post
piwib
post
Post #8





Grupa: Zarejestrowani
Postów: 51
Pomógł: 0
Dołączył: 7.11.2005

Ostrzeżenie: (0%)
-----


Ale ja zadałem to pytanko a nie ty (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif) więc ja potrzebuje a nie ty (IMG:http://forum.php.pl/style_emoticons/default/biggrin.gif)
Go to the top of the page
+Quote Post
revyag
post
Post #9





Grupa: Przyjaciele php.pl
Postów: 2 258
Pomógł: 16
Dołączył: 21.09.2004
Skąd: Kielce

Ostrzeżenie: (0%)
-----


Cytat(piwib @ 2006-02-05 23:34:40)
A jak byś to zrobił z polem tekstowym type=text (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)

np. tak
Kod
function isNotEmpty(elem) {
    var str = document.getElementById(elem).value;
    if(str == null || str.length == 0) {
        return false;
    } else {
        return true;
    }
}
Go to the top of the page
+Quote Post
piwib
post
Post #10





Grupa: Zarejestrowani
Postów: 51
Pomógł: 0
Dołączył: 7.11.2005

Ostrzeżenie: (0%)
-----


Mam to tak:

Kod
<form action="dalej.php" method="POST">
<input type=text name="elem" id="elem"/>
<br><br>
<input type=submit value="DALEJ"/>
</form>

<script type="text/javascript">
function isNotEmpty(elem) {
   var str = document.getElementById(elem).value;
   if(str == null || str.length == 0) {
       return false;
   } else {
       return true;
   }
}
</script>


I bez reakcji... Co źle lub czego brakuje?
Go to the top of the page
+Quote Post
revyag
post
Post #11





Grupa: Przyjaciele php.pl
Postów: 2 258
Pomógł: 16
Dołączył: 21.09.2004
Skąd: Kielce

Ostrzeżenie: (0%)
-----


Podałem Ci funkcję to sprawdzenia czy element jest pusty. Jej użycie nie polega na skopiowaniu jej treści gdzieś w kod i czekaniu aż "zadziała". Nie masz jej nigdzie wywoływanej. Możesz to robić przy submicie formularza:
  1. <form action="dalej.php" method="POST" onsubmit="isNotEmpty('elem')">
  2. <input type=text name="elem" id="elem"/>
  3. <br><br>
  4. <input type=submit value="DALEJ"/>
  5. </form>

Formularz nie submitnie się jeśli pole będzie puste.

ps. używaj odpowiedniego bbcode, html do kodu html, code dla js.
Go to the top of the page
+Quote Post
piwib
post
Post #12





Grupa: Zarejestrowani
Postów: 51
Pomógł: 0
Dołączył: 7.11.2005

Ostrzeżenie: (0%)
-----


  1. <form action="dalej.php" method="POST" onsubmit="isNotEmpty('elem')">
  2. <input type=text name="elem" id="elem"/>
  3. <br><br>
  4. <input type=submit value="DALEJ"/>
  5. </form>


Kod
<script type="text/javascript">
function isNotEmpty(elem) {
  var str = document.getElementById(elem).value;
  if(str == null || str.length == 0) {
      return false;
  } else {
      return true;
  }
}
</script>


I dalej nie działa... Mistrzu?!
Go to the top of the page
+Quote Post
revyag
post
Post #13





Grupa: Przyjaciele php.pl
Postów: 2 258
Pomógł: 16
Dołączył: 21.09.2004
Skąd: Kielce

Ostrzeżenie: (0%)
-----


Mały szczegół (IMG:http://forum.php.pl/style_emoticons/default/snitch.gif)
  1. <form action="dalej.php" method="POST" onsubmit="return isNotEmpty('elem')">
Go to the top of the page
+Quote Post
piwib
post
Post #14





Grupa: Zarejestrowani
Postów: 51
Pomógł: 0
Dołączył: 7.11.2005

Ostrzeżenie: (0%)
-----


Teraz to tak :] I jeszcze jedno pytanko korzystajac z topica jak można połączyć te dwa elementy?

  1. onsubmit="return formCheck();"
  2.  
  3. onsubmit="return isNotEmpty('nazwa_ankiety')"
Go to the top of the page
+Quote Post
revyag
post
Post #15





Grupa: Przyjaciele php.pl
Postów: 2 258
Pomógł: 16
Dołączył: 21.09.2004
Skąd: Kielce

Ostrzeżenie: (0%)
-----


Na szybko sklecone (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
W tablicy elems są elementy które walidujesz, pierwsze pole to id, drugie typ.
W pętli sprawdzanie czy pola wypełnione/zaznaczone - jak chcesz to sobie możesz coś tam dodać.
Zmienna result - na starcie pokazuje ile elementów ma tablica - wszystkie muszą być zaznaczone żeby formularz przeszedł.
Kod
<script type="text/javascript">
function formCheck() {
    
    var elems = new Array(
        new Array("elem1","text"),
        new Array("elem2","text"),
        new Array("elem3","checkbox"),
        new Array("elem4","checkbox")
    );
    
    var result=elems.length;
    
    for(var i=0; i<elems.length; i++) {
        switch(elems[i][1]) {
            case "text": {
                if(document.getElementById(elems[i][0]).value == null || document.getElementById(elems[i][0]).value.length == 0) {
                    result--;
                }
            }
            break;
            case "checkbox": {
                if(document.getElementById(elems[i][0]).checked==false) {
                    result--;
                }
            }
        }
    }
    
    return (result == elems.length) ? true : false;
}
</script>

  1. <form action="" method="POST" onsubmit="return formCheck()">
  2. <input id="elem1" type="text" name="elem1" />
  3. <input id="elem2" type="text" name="elem2" />
  4. <input id="elem3" type="checkbox" name="elem3" />
  5. <input id="elem4" type="checkbox" name="elem4" />
  6. <br /><br />
  7. <input type="submit" value="DALEJ"/>
  8. </form>
Go to the top of the page
+Quote Post
piwib
post
Post #16





Grupa: Zarejestrowani
Postów: 51
Pomógł: 0
Dołączył: 7.11.2005

Ostrzeżenie: (0%)
-----


Ooo bosko thank you master (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

Chciałbym jeszcze dodać alert ale coś robie źle gdyż scrypt sie nie wykonuje. Jakbyś mógł jeszcze dopisac... (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)

Ten post edytował piwib 9.02.2006, 18:16:39
Go to the top of the page
+Quote Post
revyag
post
Post #17





Grupa: Przyjaciele php.pl
Postów: 2 258
Pomógł: 16
Dołączył: 21.09.2004
Skąd: Kielce

Ostrzeżenie: (0%)
-----


Na końcu funkcji zamiast tego returna daj to:
Kod
if(result != elems.length) {
    alert("Musisz wypełnić wszystkie pola");
    return false;
} else {
    return true;
}
Go to the top of the page
+Quote Post
piwib
post
Post #18





Grupa: Zarejestrowani
Postów: 51
Pomógł: 0
Dołączył: 7.11.2005

Ostrzeżenie: (0%)
-----


Mam coś takiego:

  1. <form action="konfigurator_2.php" method="POST" onsubmit="return formCheck()">
  2.  
  3. Podaj nazwę ankiety:
  4. <input type=text name="nazwa_ankiety" id="nazwa_ankiety"><br><br>
  5. Wybierz ilość pytań ankiety:
  6. <br><br>
  7. <input type=radio name=ilosc_pytan value="3" id="p1"/>3 pytania<br>
  8. <input type=radio name=ilosc_pytan value="5" id="p2"/>5 pytań<br>
  9. <input type=radio name=ilosc_pytan value="7" id="p3"/>7 pytań<br>
  10. <input type=radio name=ilosc_pytan value="10" id="p4"/>10 pytań<br><br>
  11. <input type=submit value="Dalej"/>
  12. </form>



Kod
<script type="text/javascript">
function formCheck() {
  
   var elems = new Array(
       new Array("nazwa_ankiety","text"),
       new Array("p1","radio"),
    new Array("p2","radio"),
    new Array("p3","radio"),
       new Array("p4","radio")
   );
  
   var result=elems.length;
  
   for(var i=0; i<elems.length; i++) {
       switch(elems[i][1]) {
           case "text": {
               if(document.getElementById(elems[i][0]).value == null || document.getElementById(elems[i][0]).value.length == 0) {
                   result--;
               }
           }
           break;
           case "radio": {
               if(document.getElementById(elems[i][0]).checked==false) {
                   result--;
               }
           }
       }
   }
  
   if(result != elems.length) {
   alert("Proszę wypełnić dokładnie wszystkie pola");
   return false;
} else {
   return true;
}}
</script>


I pewnie już wam sie rzuca w oczy że jest to formularz typu radio a skrypt jest nie do przejścia. Każda opcja typu radio zaznaczona być nie może a skrypt jest tak napisany... byłbym wdzięczny za odpowiedź

Ten post edytował piwib 10.02.2006, 16:21:26
Go to the top of the page
+Quote Post
revyag
post
Post #19





Grupa: Przyjaciele php.pl
Postów: 2 258
Pomógł: 16
Dołączył: 21.09.2004
Skąd: Kielce

Ostrzeżenie: (0%)
-----


Kod
<script type="text/javascript">
function formCheck() {
    
    var elems = new Array(
        new Array("elem1","text"),
        new Array("elem2","text"),
        new Array("elem3","checkbox"),
        new Array("elem4","checkbox"),
        new Array("elem5,elem6","radio")  //sztuczka:)
    );
    
    var result=elems.length;
    
    for(var i=0; i<elems.length; i++) {
        switch(elems[i][1]) {
            case "text": {
                if(document.getElementById(elems[i][0]).value == null || document.getElementById(elems[i][0]).value.length == 0) {
                    result--;
                }
            }
            break;
            case "checkbox": {
                if(document.getElementById(elems[i][0]).checked==false) {
                    result--;
                }
            }
            break;
            case "radio": {
                rad = elems[i][0].split(",",2);
                if((document.getElementById(rad[0]).checked || document.getElementById(rad[1]).checked) == false) {
                    result--;
                }
            }
            break;
        }
    }
    
    return (result == elems.length) ? true : false;
}
</script>
Go to the top of the page
+Quote Post
piwib
post
Post #20





Grupa: Zarejestrowani
Postów: 51
Pomógł: 0
Dołączył: 7.11.2005

Ostrzeżenie: (0%)
-----


No bosko (IMG:http://forum.php.pl/style_emoticons/default/smile.gif) Jeszcze jedno pytanie i już nie męcze. Jak do tego dodać by wpisywana zmienna w polu typu text była liczbą i w dodatku większą od 0 ?
Go to the top of the page
+Quote Post

2 Stron V   1 2 >
Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 23:35