PHP validate fields

  • VeX92
  • Novice
  • Novice
  • User avatar
  • Posts: 34

Post 3+ Months Ago

Hey so I am very new to PHP but learning and I am trying to make a simple contact us forum in which the user fills out the fields and it emails the fields to an email address. The only problem is I do not know of a good way to check all of my fields for invalid characters/empty fields. Here is what i have so far that gives me nothing:
Code: [ Select ]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script language="Javascript">
function doClear(Text) {
   if(Text.value == Text.defaultValue) {
     Text.value = ""
   }
}

</script>
<h3>Leave a comment for us!</h3>
First Name: <br />
<input name="Fname" type="text"> <br />
Domain name: <br />
<input name="Domain" type="text"> <br />
Comments: <br />
<textarea name="Comments" cols="21" rows="6" wrap="hard" onFocus="doClear(this)">Please insert your comments here...</textarea> <br />
<?php
function check($msg)
{
  echo "<script langauge=\"Javascript\">alert(\"".$msg."\");</script>";
}
?>
<input type="submit" name="submit" value="Submit" onFocus="<?php $name = Fname; if(strlen($name) == 0) { check("aaa"); }?>" />
</h3>
</body>
</html>
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. </head>
  5. <body>
  6. <script language="Javascript">
  7. function doClear(Text) {
  8.    if(Text.value == Text.defaultValue) {
  9.      Text.value = ""
  10.    }
  11. }
  12. </script>
  13. <h3>Leave a comment for us!</h3>
  14. First Name: <br />
  15. <input name="Fname" type="text"> <br />
  16. Domain name: <br />
  17. <input name="Domain" type="text"> <br />
  18. Comments: <br />
  19. <textarea name="Comments" cols="21" rows="6" wrap="hard" onFocus="doClear(this)">Please insert your comments here...</textarea> <br />
  20. <?php
  21. function check($msg)
  22. {
  23.   echo "<script langauge=\"Javascript\">alert(\"".$msg."\");</script>";
  24. }
  25. ?>
  26. <input type="submit" name="submit" value="Submit" onFocus="<?php $name = Fname; if(strlen($name) == 0) { check("aaa"); }?>" />
  27. </h3>
  28. </body>
  29. </html>


Any suggestions?
Thanks!
-Vex
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • CXLink
  • Expert
  • Expert
  • User avatar
  • Posts: 685
  • Loc: ATL-GA

Post 3+ Months Ago

Here is the js validation and form I have used before.

Validation
Code: [ Select ]
<script type="text/JavaScript" language="JavaScript">
<!--
function validateForm()
{
var invalid = " ";
var name = document.form1.name.value;
var subject = document.form1.subject.value;
var emailAddress = document.form1.email.value;
var emailAddressVerify = document.form1.emailc.value;

if (name == ""){
    alert("You must indicate your name");
    return false;
  }
if (emailAddress == ""){
    alert("You must enter your email address");
    return false;
  }
if (subject == "clear"){
    alert("You must select a subject");
    return false;
  }
if (document.form1.email.value.indexOf(invalid) > -1 || document.form1.emailc.value.indexOf(invalid) > -1){
    alert("No spaces allowed in email address");
    return false;
    } else
    if ( emailAddress != emailAddressVerify ){
        alert("The email addresses do not match");
        return false;
        } else
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAddress)){
        return (true)
        } else
    alert("Invalid E-mail Address! Please re-enter.")
    return (false)
    }

//-->
</script>
  1. <script type="text/JavaScript" language="JavaScript">
  2. <!--
  3. function validateForm()
  4. {
  5. var invalid = " ";
  6. var name = document.form1.name.value;
  7. var subject = document.form1.subject.value;
  8. var emailAddress = document.form1.email.value;
  9. var emailAddressVerify = document.form1.emailc.value;
  10. if (name == ""){
  11.     alert("You must indicate your name");
  12.     return false;
  13.   }
  14. if (emailAddress == ""){
  15.     alert("You must enter your email address");
  16.     return false;
  17.   }
  18. if (subject == "clear"){
  19.     alert("You must select a subject");
  20.     return false;
  21.   }
  22. if (document.form1.email.value.indexOf(invalid) > -1 || document.form1.emailc.value.indexOf(invalid) > -1){
  23.     alert("No spaces allowed in email address");
  24.     return false;
  25.     } else
  26.     if ( emailAddress != emailAddressVerify ){
  27.         alert("The email addresses do not match");
  28.         return false;
  29.         } else
  30.     if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAddress)){
  31.         return (true)
  32.         } else
  33.     alert("Invalid E-mail Address! Please re-enter.")
  34.     return (false)
  35.     }
  36. //-->
  37. </script>


Form
Code: [ Select ]
<form action="emailcon.php" name="form1" id="form1" onSubmit="return validateForm()" method="post">
          <table width="387" border="0" align="left" cellpadding="5" cellspacing="5">
           <tr class="style1">
            <td colspan="2" valign="top"></td>
            <td width="254" align="left"><div class="fmtitlesmall">* Required Fields</div></td>
           </tr>
           <tr class="style1">
            <td colspan="2" valign="top"><div align="right" class="fmtitle">Name*</div></td>
            <td width="254" align="left"><input name="name" id="name" type="text" class="fminput" size="34"/></td>
           </tr>
           <tr class="style1">
            <td colspan="2" valign="top"><div align="right" class="fmtitle">Email Address*</div></td>
            <td><input name="email" id="email" type="text" class="fminput" size="34"/></td>
           </tr>
           <tr class="style1">
            <td colspan="2" valign="top"><div align="right" class="fmtitle">Confirm Email*</div></td>
            <td><input name="emailc" id="emailc" type="text" class="fminput" size="34"/></td>
           </tr>
           <tr class="style1">
            <td colspan="2" valign="top"><div align="right" class="fmtitle">Phone</div></td>
            <td><input name="phone" id="phone" type="text" class="fminput" size="34"/></td>
           </tr>
           <tr class="style1">
            <td colspan="2" valign="top"><div align="right" class="fmtitle">Company</div></td>
            <td><input name="company" id="company" type="text" class="fminput" size="34"/></td>
           </tr>
           <tr class="style1">
            <td colspan="2" valign="top"><div align="right" class="fmtitle">Subject*</div></td>
            <td>
                <select name="subject" id="subject">
                  <option selected value="clear">Select Subject</option>
                <option value="RequestDetails">Request Details</option>
                <option value="RequestDemo">Request Demo</option>
                <option value="reqinf">Request Information</option>
                <option value="other">Other</option>
              </select>
            </td>
           </tr>
           <tr class="style1">
            <td colspan="2" valign="top"><div align="right" class="fmtitle">Message</div></td>
            <td align="left"><textarea name="message" id="message" class="fminput" cols="30" rows="6"></textarea></td>
           </tr>
           <tr class="style1">
            <td width="-3">&nbsp;</td>
            <td width="64">&nbsp;</td>
            <td align="right"><input type="submit" name="Submit" value="Submit"/></td>
           </tr>
          </table>
          </form>
  1. <form action="emailcon.php" name="form1" id="form1" onSubmit="return validateForm()" method="post">
  2.           <table width="387" border="0" align="left" cellpadding="5" cellspacing="5">
  3.            <tr class="style1">
  4.             <td colspan="2" valign="top"></td>
  5.             <td width="254" align="left"><div class="fmtitlesmall">* Required Fields</div></td>
  6.            </tr>
  7.            <tr class="style1">
  8.             <td colspan="2" valign="top"><div align="right" class="fmtitle">Name*</div></td>
  9.             <td width="254" align="left"><input name="name" id="name" type="text" class="fminput" size="34"/></td>
  10.            </tr>
  11.            <tr class="style1">
  12.             <td colspan="2" valign="top"><div align="right" class="fmtitle">Email Address*</div></td>
  13.             <td><input name="email" id="email" type="text" class="fminput" size="34"/></td>
  14.            </tr>
  15.            <tr class="style1">
  16.             <td colspan="2" valign="top"><div align="right" class="fmtitle">Confirm Email*</div></td>
  17.             <td><input name="emailc" id="emailc" type="text" class="fminput" size="34"/></td>
  18.            </tr>
  19.            <tr class="style1">
  20.             <td colspan="2" valign="top"><div align="right" class="fmtitle">Phone</div></td>
  21.             <td><input name="phone" id="phone" type="text" class="fminput" size="34"/></td>
  22.            </tr>
  23.            <tr class="style1">
  24.             <td colspan="2" valign="top"><div align="right" class="fmtitle">Company</div></td>
  25.             <td><input name="company" id="company" type="text" class="fminput" size="34"/></td>
  26.            </tr>
  27.            <tr class="style1">
  28.             <td colspan="2" valign="top"><div align="right" class="fmtitle">Subject*</div></td>
  29.             <td>
  30.                 <select name="subject" id="subject">
  31.                   <option selected value="clear">Select Subject</option>
  32.                 <option value="RequestDetails">Request Details</option>
  33.                 <option value="RequestDemo">Request Demo</option>
  34.                 <option value="reqinf">Request Information</option>
  35.                 <option value="other">Other</option>
  36.               </select>
  37.             </td>
  38.            </tr>
  39.            <tr class="style1">
  40.             <td colspan="2" valign="top"><div align="right" class="fmtitle">Message</div></td>
  41.             <td align="left"><textarea name="message" id="message" class="fminput" cols="30" rows="6"></textarea></td>
  42.            </tr>
  43.            <tr class="style1">
  44.             <td width="-3">&nbsp;</td>
  45.             <td width="64">&nbsp;</td>
  46.             <td align="right"><input type="submit" name="Submit" value="Submit"/></td>
  47.            </tr>
  48.           </table>
  49.           </form>

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 55 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.