1
votes

hi guys below is my regex to allow only alphabets and spaces with single quote and hypen in the first and last name

 $no_digit = array('firstname'=>$_POST['firstname'],'lastname'=>$_POST['lastname'],'city'=>$_POST['city']
                               ,'country'=>$_POST['country'],'state'=>$_POST['state']);
            foreach($no_digit as $index => $text){

                echo '<br>regex = ' . preg_match("#[a-zA-Z - ']#",$text);
                    $error_flag = true;
                    $error[$index] = "Wrong Character Found";


            }

i dont know it is not doing what i want please can some one show how it should be done or what am i missing or doing absolutly wrong...

1

1 Answers

2
votes
if(preg_match("~[^a-zA-Z\-']~",$text)){
    $error_flag = true;
    $error[$index] = "Wrong Character Found";
}