Jump to content


Photo

form validation...


  • Please log in to reply
4 replies to this topic

#1 somo

somo
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 05 July 2006 - 10:59 AM

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.

#2 micah1701

micah1701
  • Members
  • PipPipPip
  • Advanced Member
  • 613 posts
  • LocationEllington, CT USA

Posted 05 July 2006 - 11:27 AM

the part for checking if a username already exists is pretty simple.

<?php
$username = $_POST['username']; // define the username you are checking

include('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
}
?>

"Confidence in the face of risk."

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 05 July 2006 - 11:45 AM

For the UK Postcode I have this:
^[A-Z]{2}[0-9]{1,2} ([0-9]{1}[A-Z]{2})$
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 z

Here it is action:
$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!';
}


#4 somo

somo
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 05 July 2006 - 01:09 PM

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.

For the UK Postcode I have this:

^[A-Z]{2}[0-9]{1,2} ([0-9]{1}[A-Z]{2})$
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 z

Here it is action:
$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!';
}



#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 05 July 2006 - 01:21 PM

Something like this:
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>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users