Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Walidacja formularza (funkcje)
peja1990
post
Post #1





Grupa: Zarejestrowani
Postów: 150
Pomógł: 1
Dołączył: 4.12.2010

Ostrzeżenie: (10%)
X----


  1. $error = array();
  2. global $error;
  3.  
  4. function validateNames($value) {
  5. if(!ereg("^[A-ZŁŚ]{1}[a-ząęółśżźćń]{1,30} [A-ZŁŚ]{1}[a-ząęółśżźćń]{1,30}$", $value) && strlen($value) < 3)
  6. return $error[] = 1;
  7. }
  8.  
  9. function validateEmail($value) {
  10. if(filter_var($value, FILTER_VALIDATE_EMAIL) === FALSE && strlen($value) == 0)
  11. return $error[] = 1;
  12. }
  13.  
  14. function validateSubject($value) {
  15. if(empty($value))
  16. return $error[] = 1;
  17. }
  18.  
  19. function validateContent($value, $min) {
  20. if(empty($min)) $min = 10;
  21. if(strlen($value) < $min)
  22. return $error[] = 1;
  23. }
  24.  
  25. function validateCaptcha($code, $security) {
  26. if(strtolower($code) != strtolower($security))
  27. return $error[] = 1;
  28. }
  29.  
  30. echo validateSubject('');
  31. echo validateContent('', '');
  32. echo '>>>'.sizeof($error).'<<<'; // Tutaj chcę mieć ilość errorów, ale ciągle jest zero :(


Co tutaj więcej pisać ?! ... (IMG:style_emoticons/default/sad.gif)

Do każdej funkcji dodałem globalną zmienną $error i działa (IMG:style_emoticons/default/wink.gif)

Ten post edytował peja1990 4.05.2012, 19:45:56
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Niktoś
post
Post #2





Grupa: Zarejestrowani
Postów: 1 195
Pomógł: 109
Dołączył: 3.11.2011

Ostrzeżenie: (10%)
X----


Ciekawi mnie ja zwracasz napis o błędzie ,dla konkretnego pola.
Nie powinno być:
return $error[0] = 1;
return $error[1] = 1;
return $error[2] = 1; itd.?
Go to the top of the page
+Quote Post
peja1990
post
Post #3





Grupa: Zarejestrowani
Postów: 150
Pomógł: 1
Dołączył: 4.12.2010

Ostrzeżenie: (10%)
X----


Cytat(Niktoś @ 4.05.2012, 20:48:25 ) *
Ciekawi mnie ja zwracasz napis o błędzie ,dla konkretnego pola.
Nie powinno być:
return $error[0] = 1;
return $error[1] = 1;
return $error[2] = 1; itd.?


heh (IMG:style_emoticons/default/wink.gif)
Poprzez wywołanie funkcji echem (IMG:style_emoticons/default/smile.gif)
echo funkcja(tekst);

Aktualna wersja kodu:
  1. $error = array();
  2.  
  3. function validateNames($value) {
  4. if(!ereg("^[A-ZŁŚ]{1}[a-ząęółśżźćń]{1,30} [A-ZŁŚ]{1}[a-ząęółśżźćń]{1,30}$", $value) && strlen($value) < 3) {
  5. global $error;
  6. return $error[] = 1;
  7. }
  8. }
  9.  
  10. function validateEmail($value) {
  11. if(filter_var($value, FILTER_VALIDATE_EMAIL) === FALSE && strlen($value) == 0) {
  12. global $error;
  13. return $error[] = 1;
  14. }
  15. }
  16.  
  17. function validateSubject($value) {
  18. if(empty($value)) {
  19. global $error;
  20. return $error[] = "puste pole #1";
  21. }
  22. }
  23.  
  24. function validateContent($value, $min) {
  25. if(empty($min)) $min = 10;
  26. if(strlen($value) < $min) {
  27. global $error;
  28. return $error[] = "puste pole #2";
  29. }
  30. }
  31.  
  32. function validateCaptcha($code, $security) {
  33. if(strtolower($code) != strtolower($security)) {
  34. global $error;
  35. return $error[] = 1;
  36. }
  37. }


Gra i buczy (IMG:style_emoticons/default/smile.gif)
jeżeli chcę sprawdzić poprawność pola formularza/tekstu to wywołuję funkcję np.
  1. echo validateSubject($_POST['subject'])

jeżeli pole jest błędnie wypełnione to dostaję treść błędu (IMG:style_emoticons/default/smile.gif)

Ten post edytował peja1990 4.05.2012, 19:54:09
Go to the top of the page
+Quote Post

Posty w temacie


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: 3.10.2025 - 06:27