menios Posted December 8, 2007 Share Posted December 8, 2007 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] Quote Link to comment Share on other sites More sharing options...
Stooney Posted December 8, 2007 Share Posted December 8, 2007 try preg_match(); http://us.php.net/manual/en/function.preg-match.php Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 8, 2007 Share Posted December 8, 2007 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"; } ?> Quote Link to comment Share on other sites More sharing options...
menios Posted December 8, 2007 Author Share Posted December 8, 2007 I know about these expressions maybe i wasn't clear enough,sorry how do i add them in another file? or could i integrate them to my existing code and if yes where? thanks for your patience Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 8, 2007 Share Posted December 8, 2007 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.'; ?> Quote Link to comment Share on other sites More sharing options...
menios Posted December 8, 2007 Author Share Posted December 8, 2007 Ohh Yes I think i m getting it now i ll try to see if i can do it Thanks pocobueno Quote Link to comment Share on other sites More sharing options...
revraz Posted December 8, 2007 Share Posted December 8, 2007 Wouldn't the pregmatch also error if the value was empty? So this would be redundant right? if ($_POST['email']=='') Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 8, 2007 Share Posted December 8, 2007 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. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 8, 2007 Share Posted December 8, 2007 Ok, just wanted to make sure, because I was in the process of changing my own registration scripts and hoped it would catch empties as well. Quote Link to comment Share on other sites More sharing options...
menios Posted December 8, 2007 Author Share Posted December 8, 2007 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 :) Quote Link to comment Share on other sites More sharing options...
menios Posted December 8, 2007 Author Share Posted December 8, 2007 what do i need to add to stop users from entering any special symbols and accept only letters?? The syntax for characters only is "/[a-zA-Z]/" if im not mistaken. But if the user adds any of special characters (*&^#;/\) next to a letter it gets accepted. How can i stop this? Quote Link to comment Share on other sites More sharing options...
revraz Posted December 8, 2007 Share Posted December 8, 2007 "/^[a-zA-Z]$/" should only alow alphabet Quote Link to comment Share on other sites More sharing options...
menios Posted December 8, 2007 Author Share Posted December 8, 2007 I ve just added it and for some reason it doesn't accept anything now.. Quote Link to comment Share on other sites More sharing options...
menios Posted December 8, 2007 Author Share Posted December 8, 2007 In case anyone interested the code for accepting only alphabetical characters is "/^[\w\ \+\-\'\"]+$/" Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.