Results 1 to 4 of 4
  1. #1
    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. >>>

  2. #2
    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
    }
    ?>
    so you should get:

    PHP Code:
    $string "DK51 THT"#correct

    $string "6451 THT"#incorrect

    $string "DK51THT"#incorrect 
    and so on ...
    Hyperconfused (™)

  3. #3
    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

  4. #4
    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. >>>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •