Results 1 to 4 of 4
-
03-24-2004, 07:50 AM #1Junior Guru
- Join Date
- Aug 2003
- Location
- Walsall - UK
- Posts
- 177
Validating a string with numerical values and alphabetical characters?
Hey guys .. I'm using PHP and a mySQL database
I'm trying to validate a car registration number (a UK one).. Which is in the form of DK51 THT... two letters, two numbers and three letters.
Thing is... I want to keep my database integrity.. so is there some way I can validate that the first characters are letters.. then the PHP checks the second are numbers.. and so forth?
Is this possible?
Thanks in advance
crE<<< Please see Forum Guidelines for signature setup. >>>
-
03-24-2004, 08:21 AM #2Retired Moderator
- Join Date
- Apr 2003
- Location
- London, UK
- Posts
- 4,721
You can use a regular expression, the following should work, and also check for the space in the correct place:
PHP Code:<?php
if (eregi("^[a-zA-Z]{2}[0-9]{2}[[:space:]][a-zA-Z]{3}$", $string)) {
#format is correct
}
?>
PHP Code:$string = "DK51 THT"; #correct
$string = "6451 THT"; #incorrect
$string = "DK51THT"; #incorrect
Hyperconfused (™)
-
03-24-2004, 09:49 AM #3Web Hosting Master
- Join Date
- Jan 2003
- Location
- Perth, WA, Australia
- Posts
- 1,276
PHP Code:if (preg_match('/^[a-z]{2}\\d\\d [a-z]{3}$/i',$str)) // valid
nu-metal.org :: coming soon
-
03-25-2004, 04:57 PM #4Junior Guru
- Join Date
- Aug 2003
- Location
- Walsall - UK
- Posts
- 177
Thanks guys, it worked!!! You're a great help
crE<<< Please see Forum Guidelines for signature setup. >>>