[Javascript] Form validation issue

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[Javascript] Form validation issue

Post by ayu »

I have a function in Javascript that is supposed to validate the form data on a site I am making. Problem is that I have never taken a lot of time in learning Javascript yet, so I'm having issues with the function.

The first one works... but it can only validate one specific element in a form, and I want to be able to set some parameters to check the other elements at the same time.

works

Code: Select all

function validate_form()
{
       valid = true;

       if ( document.form.title.value == "" )
       {
                 alert ( "Du har inte fyllt i alla fälten" );
                 valid = false;
        }

    	 return valid;
}
Does not work

Code: Select all

function validate_form_two (form, a, b)
{
        valid = true;

    	if ( document.form.a.value == "" || document.form.b.value == "")
    	{
       		alert ( "Du har inte fyllt i alla fälten" );
        	valid = false;
    	}

    	return valid;
}
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Post by bad_brain »

hmmm...looks fine for me.... :-k
have you tested all cases? like form.a true, form.b true, form.a and form.b true? additionally you could add an alert for valid=false....this might give you a hint in which part the error is (if valid=false works the error must be in the valid=true part, if none works there must be a general problem like in the function declaration or in the HTML embedment.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Solved it ^^ apparently I don't have to send the elements as parameters

Code: Select all

function validate_form_gallery(theForm)
{
	valid = true;

	if(theForm.title.value == "")
	{
		alert ( "Du har inte fyllt i alla rutor" );
		valid = false;
	}
	else if(theForm.file.value == "")
	{
		alert ( "Du har inte fyllt i alla rutor" );
		valid = false;
	}

	return valid;
}
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Post by bad_brain »

thank you for reminding me why I always hated JS and Java in my studies... :lol:

User avatar
f4Gg0t_43
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 245
Joined: 13 Sep 2008, 16:00
15
Contact:

Post by f4Gg0t_43 »

bad_brain wrote:thank you for reminding me why I always hated JS and Java in my studies... :lol:
Wait, why do you hate javascript? I just started learning it yesterday, and I think it's a great language. Java I can understand hating though.

Post Reply