NickG21 Posted December 21, 2006 Share Posted December 21, 2006 Hey everyone, below i have a very basic form with only three text boxes, i am looking to make sure only text is added in the first, only digits in the second, and valid e-mail format in the third. the validations seem to work but i am unsure of how to run my if...elseif statements in order to make the form appear clean at first without displaying the error messages.[code]<html><head></head><body><form action="testing1.php" method="post"><input type="text" name="text1" value="Your Name"><br/><input type="text" name="text2" value="0"><br/><input type="test" name="email" value-"E-Mail Address"><br/><input type="submit" name="submit" value="submit"><br/></form><?php$email = $_POST['email'];$name = $_POST['text1'];$number = $_POST['text2'];// check e-mail address// display success or failure messageif (!preg_match("/^([a-zA-Z])+/",$_POST['text1'])){echo("Text Only As Your Name, Please Re-Submit");}if (!preg_match("/^([0-9])+/",$_POST['text2'])){echo("Invalid Number Scheme");}if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email'])){echo("Invalid e-mail address");}echo "Valid e-mail address, processing...";?></body></html>[/code]any help is appreciatedthank you Link to comment https://forums.phpfreaks.com/topic/31501-solved-simple-form-validation/ Share on other sites More sharing options...
HuggieBear Posted December 21, 2006 Share Posted December 21, 2006 Try this:[code]<html><head></head><body><form action="testing1.php" method="post"><input type="text" name="text1" value="Your Name"><br/><input type="text" name="text2" value="0"><br/><input type="text" name="email" value-"E-Mail Address"><br/><input type="submit" name="submit" value="submit"><br/></form><?phpif (isset($_POST['submit'])){ $email = $_POST['email']; $name = $_POST['text1']; $number = $_POST['text2']; // check e-mail address // display success or failure message if (!preg_match("/^([a-zA-Z])+/",$_POST['text1'])) { echo("Text Only As Your Name, Please Re-Submit"); } if (!preg_match("/^([0-9])+/",$_POST['text2'])) { echo("Invalid Number Scheme"); } if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email'])) { echo("Invalid e-mail address"); } echo "Valid e-mail address, processing...";}?></body></html>[/code]RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/31501-solved-simple-form-validation/#findComment-145926 Share on other sites More sharing options...
NickG21 Posted December 21, 2006 Author Share Posted December 21, 2006 it work well, thank you Link to comment https://forums.phpfreaks.com/topic/31501-solved-simple-form-validation/#findComment-145942 Share on other sites More sharing options...
HuggieBear Posted December 21, 2006 Share Posted December 21, 2006 No problem, don't forget to mark this topic as solved using the new 'Solved' button.RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/31501-solved-simple-form-validation/#findComment-145945 Share on other sites More sharing options...
obsidian Posted December 21, 2006 Share Posted December 21, 2006 I would personally recommend you do your processing first, and then display the error [b]above[/b] the form to assure that the user will see the error message. Try something like this:[code]<?phpif (isset($_POST['submit'])) { $error = array(); if (!preg_match('|^[a-z]+$|i', $_POST['text1'])) $error[] = "First textfield may only contain letters"; if (!preg_match('|^[0-9]+$|i', $_POST['text2'])) $error[] = "Second textfield may only contain numbers"; if (!preg_match('|^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $_POST['text3'])) $error[] = "Third textfield must contain a valid email address"; if (count($error) > 0) { $msg = "<p class=\"error\">The following errors occurred:<br />\n" . implode('<br />\n', $error) . "</p>\n"; } else { // Success! }}// Show your message if there is oneecho isset($msg) ? $msg : '';// Now, display your form?>[/code]Also, make note that your regular expressions did not have the terminating string '$' that is needed to assure that they contain [b]only[/b] those characters. As you have them currently checking, they only have to [b]start[/b] with one or more of the specified characters.Hope this helps. Link to comment https://forums.phpfreaks.com/topic/31501-solved-simple-form-validation/#findComment-145948 Share on other sites More sharing options...
NickG21 Posted December 21, 2006 Author Share Posted December 21, 2006 obsidian, i agree that works much better and is much more manageable, however I receive this error after trying to put that code into mineWarning: implode(): Bad arguments. in .../testing1.php on line 16[code]<?phpif (isset($_POST['submit'])) {$email = $_POST['email'];$name = $_POST['text1'];$number = $_POST['text2'];$error = array();// check e-mail address// display success or failure messageif (!preg_match("/^([a-zA-Z])+/",$_POST['text1'])) $error[] = "Your Name May Only Contain Letters";if (!preg_match("/^([0-9])+/",$_POST['text2'])) $error[] = "Invalid ZipCode, Please Re-Enter";if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email'])) $error = "Invalid E-Mail Address";if (count($error)>0){$msg = "<p class=\"errpr\">The Following Errors Were Found:<br/>\n". implode('<br />\n', $error). "</p>\n";}else{ echo isset($msg) ? $msg: 'Thank You, Your Information Has Been Accepted';}}?>[/code] Link to comment https://forums.phpfreaks.com/topic/31501-solved-simple-form-validation/#findComment-145966 Share on other sites More sharing options...
obsidian Posted December 21, 2006 Share Posted December 21, 2006 I believe it should be:[code]<?phpif (isset($_POST['submit'])) { $email = $_POST['email']; $name = $_POST['text1']; $number = $_POST['text2']; $error = array(); // check e-mail address // display success or failure message if (!preg_match("/^([a-zA-Z])+/",$name)) $error[] = "Your Name May Only Contain Letters"; if (!preg_match("/^([0-9])+/",$number)) $error[] = "Invalid ZipCode, Please Re-Enter"; if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $email)) $error[] = "Invalid E-Mail Address"; if (count($error)>0) { $msg = "<p class=\"errpr\">The Following Errors Were Found:<br/>\n". implode('<br />\n', $error). "</p>\n"; } else { $msg = "<p>Thank You, Your Information Has Been Accepted</p>"; }}echo isset($msg) ? $msg : '';?>[/code] Link to comment https://forums.phpfreaks.com/topic/31501-solved-simple-form-validation/#findComment-145984 Share on other sites More sharing options...
NickG21 Posted December 21, 2006 Author Share Posted December 21, 2006 Works great.. thank you very much Link to comment https://forums.phpfreaks.com/topic/31501-solved-simple-form-validation/#findComment-146003 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.