Page 1 of 1

Form validation?

Posted: Thu Jun 06, 2002 7:32 am
by jmandango
I've seen this done before....I think it's using frontpage extensions which I wan't to stay away from. I want to be sure the user enters information into the form correctly and they receive a specific warning if they don't.

Code: Select all

<script Language="JavaScript"><!--
function FrontPage_Form1_Validator(theForm)
&#123;

  if (theForm.first_name.value == "")
  &#123;
    alert("Please enter a value for the "First Name" field.");
    theForm.first_name.focus();
    return (false);
  &#125;

  if (theForm.first_name.value.length < 1)
  &#123;
    alert("Please enter at least 1 characters in the "First Name" field.");
    theForm.first_name.focus();
    return (false);
  &#125;

  if (theForm.first_name.value.length > 50)
  &#123;
    alert("Please enter at most 50 characters in the "First Name" field.");
    theForm.first_name.focus();
    return (false);
  &#125;
 return (true);
&#125;
//--></script>

Posted: Thu Jun 06, 2002 8:38 am
by Johnm
I guess I do not see the question in your post. Can you clarify it please?




Direwolf

Posted: Thu Jun 06, 2002 9:10 am
by jmandango
My question is. Can I do this type of thing using PHP or even a similar JAVASCRIPT code? If so, how?

thanks

Posted: Thu Jun 06, 2002 9:30 am
by Johnm
Yes, you can do this with either PHP os JavaScript, but you should realize that it IS JavaScript. The reference to Front Page (FrontPage_Form1_Validator) is only the name of the function that validates the form. It could be called anything but the person that wrote it gave it a meaningful function name... meaningful to him that is. Does this code work the way you have it written?


Direwolf

Posted: Thu Jun 06, 2002 9:44 am
by jmandango
No it doesn't work in this manner. Help pretty please.

Posted: Thu Jun 06, 2002 9:51 am
by twigletmac
It might be easier if we knew what your form looked like.

Mac

Posted: Thu Jun 06, 2002 9:59 am
by jmandango
Here is my code
I want to be able to give a warning if they don't enter info correctly.

Code: Select all

<?php

if ($submit) &#123;

  // process form

  $db = mysql_connect("localhost", "user", "pass");
  
  mysql_select_db("database",$db);
  
  $sql = "INSERT INTO table (first_name,last_name,etc) VALUES ('$first_name','$last_name')";
  
  $result = mysql_query($sql);
  
  echo '<table align="center" cellpadding="3"><tr><td align="center" class="graybold">Thank you '.$_POST&#1111;'first_name'].' , for voting!<br>Please look for the results in our September issue.</td></tr></table>';

&#125; else&#123;


	// display form
	?>

<form method="post" action="<?php echo $PHP_SELF?>">
	
	<div align="center">
	<table width="450" border="1" cellspacing="0" cellpadding="3" class="graybody" bordercolor="#999999">
            <tr> 
              <td colspan="2" align="center">Please Enter Your Personal Information</td>
            </tr>
            <tr> 
              <td width="150" align="right">First / Last Name</td>
              <td> 
                <input type="text" name="first_name" size="20">
                * 
                <input type="text" name="last_name" size="20">
                * </td>
            </tr>
            <tr>
            <td> 
            
              <input type="submit" name="submit" value="Enter Information">
              <input type="reset">
            
          </td>
        </tr>
      </table>
</form>
	
	
	<?php
	
&#125; // end if

?>

Posted: Thu Jun 06, 2002 11:23 am
by MattF
try adding this before your query:

Code: Select all

<?php
if (empty($firstname)) || empty($second_name)) &#123;
    echo "Form not filled out completely";
&#125; else &#123;
mysql_query etc...
&#125;
?>