只允许大写字母和小写字母 [英] Allow only capital and small letters
问题描述
我尝试了下面的代码,它回应无效的字符消息,但不起作用。我的意思是它没有检查。它只是显示消息。任何帮助?
< form action =method =post>
< input type =textname =fname>
< input type =submitvalue =Sendname =submit>
< / form>
更新:这是我必须检查并将名称数据库。如果名称中的数字通过显示错误消息来拒绝该名称,否则如果该名称仅包含字母,则将其插入数据库中。
<?php
if(isset($ _POST ['submit']) )){
$ fname = $ _POST [fname]; $!
$ b if(!preg_match('/ ^([a-zA-Z] +)$ /',$ fname)){
echoInvalid characters;
}
if(empty($ fname)){
echo'< span>名字是必需的< / 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();
}
}
?>
如果您只想检查您可以使用 ctype_alpha ,但你说过你只想接受 只有字母如果您选择接受输入,您可以:
$ fname = preg_replace('/ [^ az] / i',' ,$ FNAME);
检查后更好
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();
}
}
?>
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
这篇关于只允许大写字母和小写字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!