I would like to accept only small and capital letters from the user.
I tried the code below, it echoes an invalid character message, but it doesn’t work. I mean, he does not check. It just displays a message. Any help?
<form action="" method="post">
<input type="text" name="fname">
<input type="submit" value="Send" name="submit">
</form>
Update:, this is what I have to check and paste the name into the database. if the numbers found in the name reject the name, displaying an else error message, if the name contains only letters, insert it into the database. That is all I want to achieve.
<?php
if ( isset( $_POST['submit'] ) ) {
$fname = $_POST["fname"];
if(!preg_match ('/^([a-zA-Z]+)$/', $fname)){
echo "Invalid characters";
}
if (empty($fname)) {
echo '<span> First name is required</span>';
}
else{
$mysqli = new mysqli("localhost", "root", "", "test");
$stmt = $mysqli->prepare("INSERT INTO test (firstname) VALUES (?)");
$stmt->bind_param("s", $fname);
$stmt->execute();
$stmt->close();
$mysqli->close();
}
}
?>
source
share