Jump to content

Is there a simpler way to do this?


DrTrans

Recommended Posts

print "<script type=\"text/javascript\">";   
                                       print "function validateForm()
										{
										var x=document.forms[\"addp\"][\"input_address\"].value;
										if (x==null || x==\"\")
										  {
  											alert(\"You must enter an address.\");
											 return false;
										  }
										  var x=document.forms[\"addp\"][\"input_city\"].value;
										if (x==null || x==\"\")
										  {
  											alert(\"You must enter a city.\");
											 return false;
										  }
										  var x=document.forms[\"addp\"][\"input_state\"].value;
										if (x==null || x==\"\")
										  {
  											alert(\"You must enter a state.\");
											 return false;
										  }
										  var x=document.forms[\"addp\"][\"input_zip\"].value;
										if (x==null || x==\"\")
										  {
  											alert(\"You must enter a zip code\");
											 return false;
										  }
										var x=document.forms[\"addp\"][\"input_rent\"].value;
										if (x==null || x==\"\")
										  {
  											alert(\"You must enter an address.\");
											 return false;
										  }
										} ";
                                       print"</script>";

 

Link to comment
Share on other sites

1. to avoid silly errors that come from having to escape everything,  don't use php to echo javascript, end php, execute the javascript, then start php again

 

<?php
echo "blah";
?>
<script>
//js stuff
</script>
<?php

?>

 

2. well, you could pass the form object values as arguments, which would make for a more robust and reusable function, which is what functions are for, but either way you should definitely be assigning each value to a different variable.

3. you can for loop the document.forms['addp'] values and check for them being null or empty inside of the loop, this would reduce the amount of code needed greatly.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.