preg_match not working


#1

Could someone tell me why this code isn’t catching non-alphabetical characters.

$firstname = check_input($_POST[‘firstname’], “Please enter your First Name”);
$firstname = ucwords($firstname);
if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) {
show_error(“Name should have only alpha characters and white space”);
}

And why this code is forcing user to enter an address.

if (empty($_POST[“homeemail”])) {
$homeemail = “”;
} else {
$homeemail = check_input($_POST[‘homeemail’]);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w-]+@[\w-]+.[\w-]+)/", $homeemail)) {
show_error(“Invalid personal e-mail address”);
}

Thank you


#2

This is a great site to test this out on, https://www.phpliveregex.com/p/o04

For the email pattern, it isn’t needed. Use filter_var($email, FILTER_VALIDATE_EMAIL)


#3

Have no idea how to use https://www.phpliveregex.com/p/o04. Entered code in top left block expecting something to happen or a button to click to get errors.


#4

Just write your regex in the regex field and one or more sample lines in the text box and it will automatically try to preg_match the lines