Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/13726-form-validation/
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]
Link to comment
https://forums.phpfreaks.com/topic/13726-form-validation/#findComment-53272
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]
Link to comment
https://forums.phpfreaks.com/topic/13726-form-validation/#findComment-53278
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]
Link to comment
https://forums.phpfreaks.com/topic/13726-form-validation/#findComment-53317
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]
Link to comment
https://forums.phpfreaks.com/topic/13726-form-validation/#findComment-53319
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.