[英]User input validation only numbers and letter allowed
I have a form where users enter in car registrations so I only want to allow numbers and letters? 我有一个表格,用户输入汽车注册,所以我只想允许数字和字母?
if(preg_match("/(A-Za-z0-9]+/", $to)){
$errors[] = "Only use numbers and letters please";}
get the error... 得到错误......
Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset 12 in /homepages/28/d420353834/htdocs/regupdate.php on line 84
So I am guessing I have got it wrong. 所以我猜我弄错了。
Try: 尝试:
if(preg_match("/([A-Za-z0-9])+/", $to)){
// action ...
}
You're missing [
at start and at the end )
in regexp 你在regexp中缺少
[
开始和结束时)
You're missing [
before the A
, and a )
before the final /
in your regex. 你错过了
[
在之前A
和)
前的最后/
在你的正则表达式。 Should look like: 应该是这样的:
if (preg_match("/([A-Za-z0-9]+)/", $to)) {
Also, if you're only testing for boolean, and not collecting the matches, you could omit the ()
entirely. 此外,如果您只测试布尔值,而不是收集匹配项,则可以完全省略
()
。
改为
if(preg_match("/([A-Za-z0-9])+/", $to))
I think other answers regexps return true even if your string contains a single alphanumeric char in a string containing ALSO non-alphanumeric chars. 我认为即使你的字符串在包含ALSO非字母数字字符的字符串中包含单个字母数字字符,其他答案regexp也会返回true。 (Try with "???alpha!!123" for exaple)
(尝试用“??? alpha !! 123”作为exaple)
If you want to ensure your string contains ONLY alphanumeric chars, try this code: 如果您想确保您的字符串只包含字母数字字符,请尝试以下代码:
if (preg_match("/^([[:alnum:]])*$/", $to))
{
$errors[] = "Only use numbers and letters please";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.