Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Validating surnames with apostrophes using eregi in php

Options
  • 21-10-2008 11:06am
    #1
    Closed Accounts Posts: 38


    I want to validate last name input from a form to accept apostrophes, and dashes in double-barrel names. I'm using PHP. The following code doesn't work - it flags a lastname with an apostrophe as invalid and I can't see where I'm going wrong.
    function validate_text_with_apostrophes($text) {
       if(eregi("^[a-zA-Z]+(([\'\.\-][a-zA-Z])?[a-zA-Z]*)*$", $text)) {
            return true;
        } else {
            return false; 
        }
    }
    $valid_surname = false;
    if(validate_text_with_apostrophes($SURNAME)) {
        $valid_surname = true;
        $surname = $SURNAME;
    } else {
        $valid_surname = false;
        $surname_error = "<br /><span class=\"validationerror\"><strong>Invalid characters in surname!</strong> Please correct!</span><br />";
    }
    
    The answer is probably staring me straight in the face but I can't see it. So other eyeballs are needed!
    :(


Comments

  • Registered Users Posts: 927 ✭✭✭decob


    Hmm, just gave it a quick test there and it seems to work for me...
    Are you doing any sort of input filtering on the info from the form that could be changing the apostrophes or putting something before it. Try slapping in
    die($SURNAME);
    

    before you check it to see what exactly it is validating

    this was what i did to quickly check it.
    <?php
    
    function validate_text_with_apostrophes($text) {
       if(eregi("^[a-zA-Z]+(([\'\.\-][a-zA-Z])?[a-zA-Z]*)*$", $text)) {
            return true;
        } else {
            return false; 
        }
    }
    $valid_surname = false;
    $SURNAME = "O'connor";
    if(validate_text_with_apostrophes($SURNAME)) {
        $valid_surname = true;
        $surname = $SURNAME;
        die("valid surname");
    } else {
        $valid_surname = false;
        $surname_error = "<br /><span class=\"validationerror\"><strong>Invalid characters in surname!</strong> Please correct!</span><br />";
        die($surname_error);
    }
    
    ?>
    


  • Closed Accounts Posts: 38 PixelPixie


    decob wrote: »
    Hmm, just gave it a quick test there and it seems to work for me...
    Are you doing any sort of input filtering on the info from the form that could be changing the apostrophes or putting something before it. Try slapping in
    die($SURNAME);
    

    before you check it to see what exactly it is validating

    this was what i did to quickly check it.
    <?php
    
    function validate_text_with_apostrophes($text) {
       if(eregi("^[a-zA-Z]+(([\'\.\-][a-zA-Z])?[a-zA-Z]*)*$", $text)) {
            return true;
        } else {
            return false; 
        }
    }
    $valid_surname = false;
    $SURNAME = "O'connor";
    if(validate_text_with_apostrophes($SURNAME)) {
        $valid_surname = true;
        $surname = $SURNAME;
        die("valid surname");
    } else {
        $valid_surname = false;
        $surname_error = "<br /><span class=\"validationerror\"><strong>Invalid characters in surname!</strong> Please correct!</span><br />";
        die($surname_error);
    }
    
    ?>
    

    Thanks for the die(...) tip Decob - I hadn't thought of using that before!
    Yep - the problem was the slash added (presumably by the automatic urlencode) when POSTing the lastname value to the script. Doh!
    Wrapping the retrieved lastname variable in stripslashes() has sorted the problem thus
    $SURNAME = trim(stripslashes($_REQUEST['lastName']));
    


  • Registered Users Posts: 927 ✭✭✭decob


    glad you got it sorted. One thing i noticed, you should problably allone for a space character in case a name like 'de bruin' or 'de la hoya' was entered.


Advertisement