pras0784 Posted February 4, 2012 Share Posted February 4, 2012 Hi, I have done email validation. At present it shows invalid email address if I kept blank but in the same time inserted the records in database. I want user to stay at the same page if anything is invalid. if(!empty($_POST['emailId'])){ if(preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $_POST['emailId'])){ $records['Email']=$_POST['emailId']; } else{ $emsg="Please enter valid Email address"; }} else{ $emsg="Please enter valid Email address"; } And I have used like <tr><td>Email id</td><td><input type="text" name="emailId"></td><td><?php echo ".$emsg." ;?></td></tr> Can anybody help me in this regard? Quote Link to comment https://forums.phpfreaks.com/topic/256387-email-validation/ Share on other sites More sharing options...
Andy-H Posted February 4, 2012 Share Posted February 4, 2012 You need to check that all of your error messages are empty or not set before inserting into the database, also, you don't need regex to validate an email, you can use filter_var with the filter_validate_email flag. And you would probably benefit from exceptions, unless you wish to have multiple errors display? anyway, assuming you want multiple errors: $errors = array(); $records = array(); if ( isset($_POST['submit']) ) { // do stuff if ( !filter_var($_POST['emailId'], FILTER_VALIDATE_EMAIL) ) $errors['email'] = 'You must enter a valid email address'; // validate more stuff if ( empty($errors) ) { // insert query } } <input name="emailId" value="<?php echo stripslashes(htmlentities($_POST['emailId'], ENT_QUOTES)); ?>" ><?php echo isset($error['email']) ? $error['email'] : ''; ?> Quote Link to comment https://forums.phpfreaks.com/topic/256387-email-validation/#findComment-1314417 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.