Jump to content

Form Question


menios

Recommended Posts

My form checks for empty cases and displays an error next to the wrong field, now  how can i add  regular expressions to the existing code to check for simple things like only letters, numbers and the email. I found the expressions but i can't manage to integrade them to my code. Any help would be really appreciated

Here is my form.

[size=10pt]<table cellpadding="5" align="center"  border="0" width="95%">
<th bgcolor="#3795bc" colspan="3"><h2>Shipping & Payment Info</h2></th>
<?php
// Create an empty array to hold the error messages.
$arrErrors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['Submit'])) {
    // Each time there's an error, add an error message to the error array
    // using the field name as the key.
    if ($_POST['firstname']=='')
        $arrErrors['fname'] = 'Please provide your First Name.';
if ($_POST['lastname']=='')
        $arrErrors['lname'] = 'Please provide your Last Name.';	
if ($_POST['address1']=='')
        $arrErrors['addressa'] = 'Please provide your Address1.';	
if ($_POST['address2']=='')
        $arrErrors['addressb'] = 'Please provide your Address2.';
if ($_POST['town']=='')
        $arrErrors['town'] = 'Please provide your Town.';
if ($_POST['county']=='')
        $arrErrors['county'] = 'Please provide your County.';
    if ($_POST['postcode']=='')
        $arrErrors['postcode'] = 'Please provide your Post Code.';		
if ($_POST['phone']=='')
    $arrErrors['phone'] = 'Please provide your phone number.';
    if ($_POST['email']=='')
        $arrErrors['email'] = 'A valid email address is required.';
    if (count($arrErrors) == 0) {
        // If the error array is empty, there were no errors.
        // Insert form processing here.
    } else {
        // The error array had something in it. There was an error.
        // Start adding error text to an error string.
        $strError = '<div class="formerror1"><p><h4><img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt="">Please check the following and try again:</p></h4>';     
    }
}
?>



<?php echo $strError; ?><!--
For every form field, we do the following...

Check to see if there's an error message for this form field. If there is, 
add the formerror class to the surrounding paragraph block. The formerror
class contains the highlighted box.

Insert the contents of what the user submitted bak into the form field.

Check again to see if this field has an error message. If it does, show
the error icon and the error message next to the field.
-->
<form method="post" action="<?php echo $PHP_SELF; ?>" onreset="return confirm('Do you want to reset the form?')">
<tr ><td width="20%"> </td><td ><p<?php if (!empty($arrErrors['fname'])) echo ' class="formerror"'; ?>>
    <label for="firstname">First Name:</label>
    <input name="firstname" type="text" id="firstname" value="<?php echo $_POST['firstname'] ?>">
    <?php if (!empty($arrErrors['fname'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['fname'].'</span>'; ?></td> <td width="20%"> </td></tr>
</p><br>
<tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['lname'])) echo ' class="formerror"'; ?>>
    <label for="lastname">Last Name:</label>
    <input name="lastname" type="text" id="lastname" value="<?php echo $_POST['lastname'] ?>">
    <?php if (!empty($arrErrors['lname'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['lname'].'</span>'; ?></td><td width="20%"> </td> </tr>
</p>
<tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['addressa'])) echo ' class="formerror"'; ?>>
    <label for="address1"> Address1:</label>
    <input name="address1" type="text" id="address1" value="<?php echo $_POST['address1'] ?>">
    <?php if (!empty($arrErrors['addressa'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['addressa'].'</span>'; ?></td><td width="20%"> </td> </tr>
</p>
<tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['addressb'])) echo ' class="formerror"'; ?>>
    <label for="address2"> Address2:</label>
    <input name="address2" type="text" id="address1" value="<?php echo $_POST['address2'] ?>">
    <?php if (!empty($arrErrors['addressb'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['addressb'].'</span>'; ?></td><td width="20%"> </td> </tr>
</p>
<tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['town'])) echo ' class="formerror"'; ?>>
    <label for="town"> Town:</label>
    <input name="town" type="text" id="town" value="<?php echo $_POST['town'] ?>">
    <?php if (!empty($arrErrors['town'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['town'].'</span>'; ?></td> <td width="20%"> </td></tr>
</p>
<tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['county'])) echo ' class="formerror"'; ?>>
    <label for="county"> County:</label>
    <input name="county" type="text" id="county" value="<?php echo $_POST['county'] ?>">
    <?php if (!empty($arrErrors['county'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['county'].'</span>'; ?></td> <td width="20%"> </td></tr>
</p>
<tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['postcode'])) echo ' class="formerror"'; ?>>
    <label for="postcode"> Post Code:</label>
    <input name="postcode" type="text" id="postcode" value="<?php echo $_POST['postcode'] ?>">
    <?php if (!empty($arrErrors['postcode'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['postcode'].'</span>'; ?></td><td width="20%"> </td> </tr>
</p>
<tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['phone'])) echo ' class="formerror"'; ?>>
    <label for="phone">Phone:</label>
    <input name="phone" type="text" id="phone" value="<?php echo $_POST['phone'] ?>">
    <?php if (!empty($arrErrors['phone'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['phone'].'</span>'; ?></tr><td width="20%"> </td></tr>
</p>
<tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['email'])) echo ' class="formerror"'; ?>>
    <label for="email">Email:</label>
    <input name="email" type="text" id="email" value="<?php echo $_POST['email'] ?>">
    <?php if (!empty($arrErrors['email'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['email'].'</span>'; ?></tr><td width="20%"> </td></tr>
</p>

<p>
   <tr><td colspan="2" align="right"> <input  id="reset"  type="reset"  value="Reset"> <input  type="submit" name="Submit" value="Submit"></tr></td>
</p>
</form>[/size]

Link to comment
https://forums.phpfreaks.com/topic/80795-form-question/
Share on other sites

To check if an email is a valid format

 

<?php

if (preg_match("/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/", $email)) {
   echo "Valid";
} else {
   echo "Invalid";
}

?>

 

To check if something is numeric

 

<?php

if (is_numeric($num)){
   echo "valid";
} else {
   echo "not valid";
}

?>

 

Check for only letters

 

<?php

if (preg_match("[A-Za-z]", $string)) {
   echo "Valid";
} else {
   echo "Invalid";
}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/80795-form-question/#findComment-409836
Share on other sites

The same way you have been doing it. Here is how you would do the email format check.

 

<?php

if ($_POST['email']=='')
   $arrErrors['email'] = 'A valid email address is required.';

if (!preg_match("/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/", $_POST['email'])) 
   $arrErrors['email_format'] = 'A valid email format is required.';

?>

Link to comment
https://forums.phpfreaks.com/topic/80795-form-question/#findComment-409855
Share on other sites

Wouldn't the pregmatch also error if the value was empty?

 

So this would be redundant right?

 

if ($_POST['email']=='')

 

Yeah, thats true. So you really don't need to check if it's empty....unless they want a separate error for some reason.

Link to comment
https://forums.phpfreaks.com/topic/80795-form-question/#findComment-409863
Share on other sites

I just changed this

<?php

if ($_POST['email']=='')
   $arrErrors['email'] = 'A valid email address is required.';

to this and it works so i ll do sth similar for the rest.

if (!preg_match("/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$/", $_POST['email'])) 
   $arrErrors['email_format'] = 'A valid email format is required.';

?>

 

Thanks for the tips :):)

Link to comment
https://forums.phpfreaks.com/topic/80795-form-question/#findComment-409871
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.