somo Posted July 5, 2006 Share Posted July 5, 2006 Hi,i need validation for UK postcode field, UK telephone number field and to check with mysql datbase if there are existing users with the same username there also. These fields need checking and sending (with other data such as address etc) to a mysql database to be stored. Anyone got any ideas on how i can go about doing this? any code would be aprreciated.Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/13726-form-validation/ Share on other sites More sharing options...
micah1701 Posted July 5, 2006 Share Posted July 5, 2006 the part for checking if a username already exists is pretty simple.[code]<?php$username = $_POST['username']; // define the username you are checkinginclude('connectionStringInfo.php'); // I assume you know how to connect to your DB$query = mysql_query("SELECT username FROM usersTable WHERE username='$username' ");$rows = mysql_num_rows($query);if($rows > 0){exit("Username Already Exisits"); // stop script and return error message}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13726-form-validation/#findComment-53272 Share on other sites More sharing options...
wildteen88 Posted July 5, 2006 Share Posted July 5, 2006 For the UK Postcode I have this:[code]^[A-Z]{2}[0-9]{1,2} ([0-9]{1}[A-Z]{2})$[/code]Here is a step through of what above expression does:[A-Z]{2} - Check the first two characters of the Postal code are letters from a through to z[0-9]{1,2} - Checks that there are at least one or two characters after the first two are numbers, from 0 - 9[0-9]{1} - Does the same as above, but checks the the next character is a number after the space[A-Z]{2} - Check the last two characters of the Postal code are letters from a through to zHere it is action:[code=php:0]$regexp = "^[A-Z]{2}[0-9]{1,2} ([0-9]{1}[A-Z]{2})$";$postal = 'AB1 2BC';if(!ereg(strtoupper($regexp), $postal)){ echo '<i>'.$postal.'</i> - invalid postal code';}else{ echo '<i>'.$postal.'</i> - valid postal code!';}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13726-form-validation/#findComment-53278 Share on other sites More sharing options...
somo Posted July 5, 2006 Author Share Posted July 5, 2006 How would you use this in relation to a html post code field? Sorry im new to php and just trying to teach myself the language bit by bit.thanks.[quote author=wildteen88 link=topic=99508.msg391867#msg391867 date=1152099904]For the UK Postcode I have this:[code]^[A-Z]{2}[0-9]{1,2} ([0-9]{1}[A-Z]{2})$[/code]Here is a step through of what above expression does:[A-Z]{2} - Check the first two characters of the Postal code are letters from a through to z[0-9]{1,2} - Checks that there are at least one or two characters after the first two are numbers, from 0 - 9[0-9]{1} - Does the same as above, but checks the the next character is a number after the space[A-Z]{2} - Check the last two characters of the Postal code are letters from a through to zHere it is action:[code=php:0]$regexp = "^[A-Z]{2}[0-9]{1,2} ([0-9]{1}[A-Z]{2})$";$postal = 'AB1 2BC';if(!ereg(strtoupper($regexp), $postal)){ echo '<i>'.$postal.'</i> - invalid postal code';}else{ echo '<i>'.$postal.'</i> - valid postal code!';}[/code][/quote] Quote Link to comment https://forums.phpfreaks.com/topic/13726-form-validation/#findComment-53317 Share on other sites More sharing options...
wildteen88 Posted July 5, 2006 Share Posted July 5, 2006 Something like this:[code]if(isset($_POST['submit'])){ if(!ereg("^[A-Z]{2}[0-9]{1,2} ([0-9]{1}[A-Z]{2})%body%quot;, strtoupper($_POST['postal']))) { echo '<i>'.$_POST['postal'].'</i> - invalid postal code'; } else { echo '<i>'.$_POST['postal'].'</i> - valid postal code!'; }}?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" name="postal" value="<?php echo @$_POST['postal']; ?>"/><br /> <input type="submit" name="submit" value="Submit" /></form>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13726-form-validation/#findComment-53319 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.