Validating forms

Validating multiple input tags with if statement

Page 1 of 1

7 Replies - 781 Views - Last Post: 16 May 2009 - 11:48 AM

#1 graphichill  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 17-February 09

Validating forms

Posted 16 May 2009 - 07:24 AM

Im trying to validate my form but i can only do one at a time because of the if & else if statement, can someone show me a way to return false on all input fields if theres nothing in any of them, not just the first one.
	  <form name="form_quote" action="mail.php" method="post" onsubmit="return validate();">
		<label for="company">Company</label><input type="text" id="company" name="comp" onfocus="blank()" /><br />
		<label for="name">Name</label><input type="text" id="name"  name="name" onfocus="blank()" /><br />
		<label for="telephone">Telephone</label><input type="text" id="telephone"  name="telephone" onfocus="blank()" /><br />
		<label for="email">Email</label><input type="text" id="email"  name="email" onfocus="blank()" /><br />
		<label for="req">Requirements</label><textarea rows="10" cols="50" id="req" name="req" onfocus="blank()" ></textarea>   <br />
		<input type="submit" value="Submit"  id="submit" />
	  </form>


function validate(){

  var text="Required Field"

	if ( document.form_quote.comp.value == "" || document.form_quote.comp.value == text)
	{   document.form_quote.comp.value = text;
		return false;
	}
	
	 else if (document.form_quote.name.value == "" || document.form_quote.name.value == text)
	 { document.form_quote.name.value = text;
		return false;
		
	  }
	
	
	else if (document.form_quote.telephone.value == "" || document.form_quote.telephone.value == text)
	   {document.form_quote.telephone.value = text;
		return false;
	  }
		
	
	
   else if (document.form_quote.email.value == "" || document.form_quote.email.value == text)
	  {document.form_quote.email.value = text;
		return false;
	}
		
	
	
	else if (document.form_quote.req.value == "" || document.form_quote.req.value == text)
	  {document.form_quote.req.value = text;
		return false;
	  }
		

	else {return true;}  
}


function blank(){
if(document.form_quote.comp.value == 'Required Field') {document.form_quote.comp.value=''}
else if(document.form_quote.name.value == 'Required Field') {document.form_quote.name.value=''}
else if(document.form_quote.telephone.value == 'Required Field') {document.form_quote.telephone.value=''}
else if(document.form_quote.email.value == 'Required Field') {document.form_quote.email.value=''}
else if(document.form_quote.req.value == 'Required Field') {document.form_quote.req.value=''}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Validating forms

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Validating forms

Posted 16 May 2009 - 07:25 AM

remove the else's from your statements. This will turn them into separate if statements which will all be run one after another.
Was This Post Helpful? 0
  • +
  • -

#3 graphichill  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 17-February 09

Re: Validating forms

Posted 16 May 2009 - 07:41 AM

This still dont work

see here clients.graphichill.com/stage/globalemc
Was This Post Helpful? 0
  • +
  • -

#4 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Validating forms

Posted 16 May 2009 - 08:18 AM

Hi, as William_Wilson says, you shouldn't use else/if statements only if's and you should add an error-parameter or something to check how many errors occured if none return true else false:
function validate()
{
	var text   = "Required Field";
	var errors = 0;

	if ( document.form_quote.comp.value == "" || document.form_quote.comp.value == text)
	{
		document.form_quote.comp.value = text;
		errors++;
	}

	if (document.form_quote.name.value == "" || document.form_quote.name.value == text)
	{
		document.form_quote.name.value = text;
		errors++;
	}

	if (document.form_quote.telephone.value == "" || document.form_quote.telephone.value == text)
	{
		document.form_quote.telephone.value = text;
		errors++;
	}

	if (document.form_quote.email.value == "" || document.form_quote.email.value == text)
	{
		document.form_quote.email.value = text;
		errors++;
	}

	if (document.form_quote.req.value == "" || document.form_quote.req.value == text)
	{
		document.form_quote.req.value = text;
		errors++;
	}

	if(errors == 0)
	{
		return true;
	}
	return false; 
}


Hope it helps! :)

This post has been edited by Wimpy: 16 May 2009 - 08:20 AM

Was This Post Helpful? 1
  • +
  • -

#5 graphichill  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 17-February 09

Re: Validating forms

Posted 16 May 2009 - 08:24 AM

This was very helpful, I cant thank you enough, what does a man have to do to get to your standard.

Thanks again
Was This Post Helpful? 0
  • +
  • -

#6 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Validating forms

Posted 16 May 2009 - 08:37 AM

Just practice a day or two and you should be fine! :) I love the design of your site,( by the way,) it's very nice!

View Postgraphichill, on 16 May, 2009 - 05:24 PM, said:

This was very helpful, I cant thank you enough, what does a man have to do to get to your standard.

Thanks again

Was This Post Helpful? 0
  • +
  • -

#7 graphichill  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 17-February 09

Re: Validating forms

Posted 16 May 2009 - 10:35 AM

Thank you for your kind comments If you ever need a designer im at www.graphichill.com
Was This Post Helpful? 0
  • +
  • -

#8 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Validating forms

Posted 16 May 2009 - 11:48 AM

I'll remember that! :)

View Postgraphichill, on 16 May, 2009 - 07:35 PM, said:

Thank you for your kind comments If you ever need a designer im at www.graphichill.com

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1