Jump to content

Archived

This topic is now archived and is closed to further replies.

somo

form validation...

Recommended Posts

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.

Share this post


Link to post
Share on other sites
the part for checking if a username already exists is pretty simple.

[code]<?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
}
?>[/code]

Share this post


Link to post
Share on other sites
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 z

Here 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]

Share this post


Link to post
Share on other sites
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 z

Here 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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.