Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Sprawdzanie formularza rozbijanie na funkcje
imosek8
post 8.09.2013, 11:29:37
Post #1





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 26.08.2013

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


Witam! Tworze formularz, sprawdzam go juz przed wyslaniem formularza, aby dynamicznie pokazywalo co jest zle wpisane.
mam cos takiego:
to jest w inpucie:
  1. <input type="text" name="login" placeholder="Login" class="forlog2" id="login" onclick="checking('login','3','10')" onkeyup="checking('login','3','10')">


a funkcja wygląda tak:

  1. function checking(id,min,max)
  2. {
  3. var x = document.getElementById(id);
  4. var length = parseInt(x.value.length);
  5. if(x.value ==''){
  6. x.removeError();
  7. x.createTitleError('To pole musi być wypełnione.');
  8. return false;
  9. }
  10. if(length <min)
  11. {
  12. x.removeError();
  13. x.createTitleError('Minimalna długość pola to '+min+' znaki.');
  14. return false;
  15. }
  16. if(length >max)
  17. {
  18. x.removeError();
  19. if(max>=5){
  20. x.createTitleError('Maksymalna długość pola to '+max+' znaków.');}
  21. if(max>=5){
  22. x.createTitleError('Maksymalna długość pola to '+max+' znaki.');}
  23. return false;
  24. }
  25. else{
  26. x.removeError();
  27. return true;
  28. }
  29. }


no i mi to ladnie dziala....

ale jak juz rozbijam to na mniejsze funkcje to nie dziala to wyglada tak:

input zostaje ten sam, a funkcje:

  1. function checkIsEmpty(id){
  2. var x = document.getElementById(id);
  3. if(x.value ==''){
  4. x.removeError();
  5. x.createTitleError('To pole musi być wypełnione.');
  6. return false;
  7. }
  8.  
  9. function checking(id,min,max)
  10. {
  11. var x = document.getElementById(id);
  12. var length = parseInt(x.value.length);
  13. checkIsEmpty(id);
  14. if(length <min)
  15. {
  16. x.removeError();
  17. x.createTitleError('Minimalna długość pola to '+min+' znaki.');
  18. return false;
  19. }
  20. if(length >max)
  21. {
  22. x.removeError();
  23. if(max>=5){
  24. x.createTitleError('Maksymalna długość pola to '+max+' znaków.');}
  25. if(max>=5){
  26. x.createTitleError('Maksymalna długość pola to '+max+' znaki.');}
  27. return false;
  28. }
  29. else{
  30. x.removeError();
  31. return true;
  32. }
  33. }
  34. }

to juz mi nie dziala... co powinienem zrobic aby działało?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 1)
PaFaT
post 17.09.2013, 20:10:25
Post #2





Grupa: Zarejestrowani
Postów: 30
Pomógł: 3
Dołączył: 27.03.2012

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


Drogi(a) imosek8,

w funkcji checkIsEmpty brakuje nawiasu } kończącego funkcję.

Poprawny kod jest następujący:

function checkIsEmpty(id){
var x = document.getElementById(id);
if(x.value ==''){
x.removeError();
x.createTitleError('To pole musi być wypełnione.');
return false;
}
}
 
function checking(id,min,max)
{
var x = document.getElementById(id);
var length = parseInt(x.value.length);
checkIsEmpty(id);
if(length <min)
{
x.removeError();
x.createTitleError('Minimalna długość pola to '+min+' znaki.');
return false;
}
if(length >max)
{
x.removeError();
if(max>=5){
x.createTitleError('Maksymalna długość pola to '+max+' znaków.');}
else{
x.createTitleError('Maksymalna długość pola to '+max+' znaki.');}
return false;
}
else{
x.removeError();
return true;
}
}
}

Poza tym, w odwołaniu do funkcji checking() w elemencie input liczby 3 i 10 powinny być napisane bez apostrofów:

onkeyup="checking('login', 3, 10)"

Powinno zadziałać. smile.gif
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: 26.06.2025 - 14:58