Suppose I have a form and in the form, there is an input field for the users to put their full names. I want only characters (A-Za-z) and spaces to be submitted by the users.
<form action='page2.php'>
<input type='text' name='fullname'>
<input type='submit' name='submit' value='Submit'>
</form>
I know, it can be done by html. But I want to check in page2 if user has typed anything without (A-Za-z) and spaces. How this check can be performed with php?
Try this
if (!preg_match("/^[a-zA-Z]$/", $user)) {
/// not match
}
Regex is big for this kind of tasks. You can use this :
if (ctype_alpha(str_replace(' ', '', $name)) === false) {
$errors[] = 'Name must contain letters and spaces only';
}
if you want to use regex then below is the code to check alphabet only
preg_match('/^[a-zA-Z]+$/', $string);