Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow only capital and small letters

Tags:

html

php

I would like to accept only small and capital letters from the user.

I tried the below code, it echoes the invalid character message but doesn't work. I mean it doesn't check. It just displays the 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 insert the name to database. if numbers found in the name reject the name by displaying the error message else if the name contains only letters insert it into database. That's all I want to acheive.

<?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();


}
}
?>
like image 911
alte Avatar asked Feb 06 '13 04:02

alte


People also ask

What are capital letters called?

Capital letters are formally know as upper case letters, from the case in which hand set type was kept at printers. It is is normally positioned above the case containing the “small" letters, which are formally known as lower case. this is written in all lower case letters.

What is the difference between small and capital letters?

These are also called lower case by the printing press staff. A capital letter was called ‘upper case’ on old fashioned typewriters when you had to press the ‘shift’ key to write a capital letter rather than the ‘Caps Lock’ nowadays. Capital letters are: ABCD etc. Small letters are: abcd etc. How do you change a small letter to a capital letter?

Why does the input box only allow upper case letters?

To make it easier on the user and prevent calls that it isn't working, they can control the input box so it only allows upper case letters and set case sensitivity to require all upper case letters.

What characters are not allowed in an expression?

For example: Anything containing other characters is not okay. That is numbers, or any other symbols. Show activity on this post. [] is a character class - any character that matches inside this expression is allowed


3 Answers

If you just want to check you could use ctype_alpha() but you said you want to ACCEPT only letters so if you choose to accept the input you could:

$fname=preg_replace('/[^a-z]/i','',$fname);

better after the check

like image 190
CSᵠ Avatar answered Oct 20 '22 08:10

CSᵠ


if(!isset($_POST['fname']) || !ctype_alpha($_POST['fname'])){
  // can i haz alpha letters only?
}

(reference)

like image 28
nice ass Avatar answered Oct 20 '22 10:10

nice ass


There are several issues with the code, and the one you are stuck with is probably that you have the form and its processing in the same PHP file. That’s possible, but it requires a different approach. For a starter, it’s probably better to separate them.

What happens with the code posted is that the PHP processor tries to process the form data when no form has been submitted, without even checking for the presence of the data. Now $fname is undefined, so the test always fails.

The test is wrong, too. Now it only checks whether $fname contains at least one letter. For example, if(!preg_match ('/^[a-zA-Z]+$/', $fname)) would test that $fname consists of one or more Ascii letters and nothing else.

like image 44
Jukka K. Korpela Avatar answered Oct 20 '22 10:10

Jukka K. Korpela