PHP- 如何在文本字段中只允许字母。还有如何只允许文本字段中的数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37052737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP- how to allow only letters in a text field. Also how to only allow numbers in a text field
提问by Shiv Patel
How do I add a input field that only accepts letters. Also how do I add a input field that only accepts numbers. And once the user inputs say letters in a text field that only accepts numbers it gives an error.
如何添加仅接受字母的输入字段。另外我如何添加一个只接受数字的输入字段。一旦用户在只接受数字的文本字段中输入字母,就会出现错误。
if(empty($_POST['firstname']))
{
$errors['firstname1'] = " Required";
}
if(empty($_POST['zip']))
{
$errors['zip1'] = " Required";
}
<!-- Letters only for firstname -->
<p>
<label for="firstname" class="label"><font color="#040404">*First Name:</font></label>
<input class="textinput" id="firstname" type="text" name="firstname" value="<?php if(isset($_POST['firstname'])) echo $_POST['firstname']; ?>" /><?php if(isset($errors['firstname1'])) echo $errors['firstname1']; ?>
</p>
<!-- Letters only for zip -->
<p>
<label for="zip" class="label"><font color="#040404">*Zip Code:</font></label>
<input id="zip" type="text" name="zip" value="<?php if(isset($_POST['zip'])) echo $_POST['zip']; ?>" /><?php if(isset($errors['zip1'])) echo $errors['zip1']; ?> <?php if(isset($errors['zip2'])) echo $errors['zip2']; ?>
</p>
回答by sktwentysix
Consider using the PHP functions ctype_alpha& ctype_digitin your form validation.
考虑在表单验证中使用 PHP 函数ctype_alpha和ctype_digit。
ctype_alphareturns true only if validation consists of letters, and will return false if numeric or special characters are used.
ctype_alpha仅在验证由字母组成时返回 true,如果使用数字或特殊字符则返回 false。
ctype_digitreturns true only if validation consists of numbers, and will return false if alphabetic or special characters are used.
ctype_digit仅在验证由数字组成时返回 true,如果使用字母或特殊字符,则返回 false。
Here's a quick example that will check an array. It will return false since one of the strings has a number in it:
这是一个检查数组的快速示例。它将返回 false,因为其中一个字符串中有一个数字:
$string = array('word', 'number5');
if (ctype_digit($string)) {
echo "All numbers are true!";
} else {
echo "Something's not right";
}
Hope this helps!
希望这可以帮助!
回答by Rajdeep Paul
how to allow only letters in a text field.
如何在文本字段中只允许字母。
You can use pattern
attribute of HTML5 to allow only letters in a text field, like this:
您可以使用pattern
HTML5 的属性来仅允许文本字段中的字母,如下所示:
<input type="text" name="fieldname1" pattern="[a-zA-Z]{1,}" required>
how to only allow numbers in a text field
如何只允许文本字段中的数字
Simillarly, make the type
attribute number
to only allow numbers in a text field, like this:
同样,使type
属性number
只允许文本字段中的数字,如下所示:
<input type="number" name="fieldname2" required>
Or, use the pattern
attribute, like this:
或者,使用该pattern
属性,如下所示:
<input type="text" name="fieldname2" pattern="[0-9]{1,}" required>
Sidenote:Even though these input fields on the client sidewill restrict users to some extent, they can't be reliable and your only line of defense. Use PHP Regexon the server sideto strictly validate your input data.
旁注:尽管客户端的这些输入字段会在一定程度上限制用户,但它们并不可靠,也是您唯一的防线。在服务器端使用PHP Regex来严格验证您的输入数据。
回答by IAmSurajBobade
You do not need to write any extra code to satisfy your requirement
您不需要编写任何额外的代码来满足您的要求
Following is a example which allows only letters (both small and capital in any sequence).
以下是仅允许使用字母(大小写任意顺序)的示例。
<input id="my_id" type="text" pattern="[A-Za-z]*" class="validate">
Or you can give more constraints like allow only first letter to be capital and reaming in small caps with pattern="[A-Z][a-z]*"
或者您可以提供更多限制,例如仅允许第一个字母为大写字母并使用小写字母进行铰孔 pattern="[A-Z][a-z]*"
To allow only numbers use pattern="[0-9]*"
and to allow only fix number (say 10) of numbers use pattern="[0-9]{10}"
只允许使用数字pattern="[0-9]*"
并只允许使用固定数字(比如 10)pattern="[0-9]{10}"
回答by Wahid Lahouiter
Use this :
用这个 :
function removeInvalidCharacters($input, $allowedChars = 'abcdefghijklmnopqrstuvwxyz 1234567890')
{
$str = '';
for ($i = 0; $i < strlen($input); $i++)
{
if (!stristr($allowedChars, $input[$i]))
{
continue;
}
$str .= $input[$i];
}
return $str;
}
回答by Mojilo
Note : This is for PHP only
注意:这仅适用于 PHP
If you Want to validate with server side with PHP
you can use:
如果您想使用服务器端进行验证,PHP
可以使用:
function validate_string_spaces_only($string) {
if(preg_match("/^[\w ]+$]/", $string)) {
return true;
} else {
return false;
}
}
string consiting only of letters, numbers and optional spaces
仅由字母、数字和可选空格组成的字符串
回答by Webomatik
For numbers only, here's a snippet I use with jQuery:
仅对于数字,这是我与 jQuery 一起使用的片段:
$('.content').keydown(function(e){
var c = (e.charCode || e.keyCode);
if(c<48 || c>57){
return false;
}else{
return true;
}
});