2 Replies - 805 Views - Last Post: 08 July 2011 - 05:20 AM

#1 hallizh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-March 08

Validating form given certain conditions[PHP, JavaScript]

Posted 08 July 2011 - 04:26 AM

Hello DreamInCode,

I have a pretty large form that needs to be validated in all sorts of ways.

Picture it like this:

------------------------------
Does the building have stairs?
[] Yes [] No
------------------------------

This is just one group of it. So anyway, if the user clicks Yes, input fields and checkboxes come dropping down. If you select No then nothing happens. The Yes and No are checkboxes for now(various reasons, just prototype for work).

The problem is validating each field. If you select "no" then all the inner fields/checkboxes are empty/unchecked and that makes it come up with a boring PHP notice when I post it to the PHP form handler(sends it to MySQL).

Here is a little code to show you how I started to think about it, but I realized there must be a more elegant way. I'm also using jquery.

function validateSkabraut()
{
	var x=document.forms["adgengiform"]["skabraut"].value;

	if(x==0)
	{
		setZero("skabraut_haed");
		setZero("skabraut_lengd");
		setZero("skabraut_handrid");
		setZero("skabraut_handrid_haed");	
	}
	if(x==1)
	{
		if(document.forms["adgengiform"]["skabraut_haed"].value == "")
		{
			alert("skabraut_haed not filled");
		}
		if(document.forms["adgengiform"]["skabraut_lengd"].value == "")
		{
			alert("skabraut_lengd not filled");
		}
		if(document.forms["adgengiform"]["skabraut_handrid"].value == "")
		{
			alert("skabraut_handrid not filled");
		}
		if(document.forms["adgengiform"]["skabraut_handrid_haed"].value == "")
		{
			alert("skabraut_handrid_haed not filled");
		}
	}
	if (x==null || x=="")
	{
		alert("You need to select Yes or No in the skabraut field.");
	}
}



Any pointers would be great! Thanks :)

Best regards,
Halli

Is This A Good Question/Topic? 0
  • +

Replies To: Validating form given certain conditions[PHP, JavaScript]

#2 arvindthakur  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 133
  • Joined: 07-June 11

Re: Validating form given certain conditions[PHP, JavaScript]

Posted 08 July 2011 - 05:17 AM

Can you also send the HTML part?
Was This Post Helpful? 0
  • +
  • -

#3 hallizh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-March 08

Re: Validating form given certain conditions[PHP, JavaScript]

Posted 08 July 2011 - 05:20 AM

Sure.

This is the part that matches the code above:

Er skábraut viđ innganginn:<br />
<input type="checkbox" name="skabraut" id="skabraut" value="1" />Já
<input type="checkbox" name="skabraut" id="skabraut" value="0" />Nei 
<div id="adkomadiv">
Skábraut hćđ: <input type="text" name="skabraut_haed" /> cm<br />
Skábraut lengd: <input type="text" name="skabraut_lengd" /> cm<br />
Handriđ:<br />
<input type="radio" name="skabraut_handrid" value="0" /> Hvergi<br />
<input type="radio" name="skabraut_handrid" value="1" /> Hćgri <br />
<input type="radio" name="skabraut_handrid" value="2" /> Vinstri<br />
<input type="radio" name="skabraut_handrid" value="3" /> Beggja meginn<br />
Hćđ handriđs: <input type="text" name="skabraut_handrid_haed" /> cm<br />



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1