Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP][JavaScript]Walidacja kilku pól formularza na raz
Grandalf00
post 4.02.2016, 07:37:34
Post #1





Grupa: Zarejestrowani
Postów: 44
Pomógł: 0
Dołączył: 23.01.2016

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


Witam! Próbuje napisać od podstaw skrypt walidujący formularze. Mam trzy pola, które chce sprawdzać w momencie naciśnięcia submita. Chyba czegoś nie rozumiem w składni JS bo skrypt powoduje tylko mignięcie podpowiedzi do pól i reszta strony wykonuje się dalej - wykonywane jest uzupełnienie bazy danych.

_____

Poradziłem sobie z problemem. W kodzie jeszcze trzeba nanieść pare usprawnień, ale ogólny zamysł jest spełniony więc zamieszczam gdyby ktoś był zainteresowany.

CODE
  1. function validate(kategorie,kwota,opis,id) {
  2. var regexp_price = /^\d+((\.|,)\d{2})?$/;
  3. var regexp_desc = /^[0-9A-Za-ząćęłńóśźżĆŁŚŹŻ,]+$/;
  4.  
  5. if (kategorie.value == "") {
  6. document.getElementsByName("1_error")[id].innerHTML = "Wybierz kategorię wpisu";
  7. return false;
  8. }
  9.  
  10. if (kategorie.value != "") {
  11. document.getElementsByName("1_error")[id].innerHTML = "";
  12. }
  13.  
  14. if (kwota.value == "" || kwota.value.match(regexp_price) == null) {
  15. document.getElementsByName("2_error")[id].innerHTML = "Podaj prawidłową kwotę";
  16. return false;
  17. }
  18.  
  19. if (kwota.value != "") {
  20. document.getElementsByName("2_error")[id].innerHTML = "";
  21. }
  22.  
  23. if (opis.value != "" && opis.value.match(regexp_desc) == null) {
  24. document.getElementsByName("3_error")[id].innerHTML = "Dozwolone litery i cyfry";
  25. return false;
  26. }
  27.  
  28. if (opis.value != "") {
  29. document.getElementsByName("3_error")[id].innerHTML = "";
  30. }
  31.  
  32. return true;
  33. }
  34.  
  35.  
  36.  
  37. window.onload = function() {
  38. document.getElementsByName("form")[0].onsubmit = function() {
  39. if (validate(this.kategorie, this.kwota, this.opis,0)) {
  40. return true;
  41. }
  42. else {
  43. return false;
  44. }
  45. }
  46.  
  47. document.getElementsByName("form")[1].onsubmit = function() {
  48. if (validate(this.kategorie, this.kwota, this.opis,1)) {
  49. return true;
  50. }
  51. else {
  52. return false;
  53. }
  54. }
  55. }


Można zamknąć.

Ten post edytował Grandalf00 4.02.2016, 10:59:38
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 7.07.2025 - 05:34