[英]php Form Validation on a textarea
I recently switched a input form from a text to a textarea so that I could have multiple lines of text. 最近,我将输入形式从文本切换为文本区域,这样我可以有多行文本。 The form is working fine except when I go to validate. 表格运行正常,除非我要进行验证。 If it is one line, there is no problem but when I enter text that wraps to the second line, i get an error from my form validator (only letters and numbers are allowed) here is my code: 如果是一行,那没有问题,但是当我输入换行到第二行的文本时,我从表单验证器收到错误(仅允许字母和数字),这是我的代码:
private function validate_alnum($var, $min=0, $max=0, $required=false, $err="") {
if($required==false && strlen($this->source[$var]) == 0) {
return true;
}
if(isset($this->source[$var])){
if(strlen($this->source[$var]) < $min) {
$this->errors[$var] = $err . ' is too short. It has to be at least '.$min. ' character(s)';
} elseif(strlen($this->source[$var]) > $max) {
$this->errors[$var] = $err . " is too long. It can't be more than ".$max. " characters";
} elseif(!preg_match("/^[a-zA-Z0-9 -.:,]*$/", $this->source[$var])) {
$this->errors[$var] = $err . ' is invalid. Only letters and numbers are allowed';
}
}
}
Your regex doesn't allow new lines. 您的正则表达式不允许换行。 Try: 尝试:
^[-a-zA-Z0-9\s.:,]*$
also -
should be the first or last character if you want it to be literal, otherwise it makes a range. 同样-
如果您希望它是字面值,则应该是第一个或最后一个字符,否则它将产生一个范围。
The \\s
here will allow for single spaces, new lines, and tabs. 此处的\\s
将允许使用单个空格,换行和制表符。 ( Additional reading on the \\s
) (有关\\s
其他阅读 )
If you change the quantifier to +
you can be sure there is more than 1 character in the string, which would take out your default $min
requirement (the error message would be less specific though). 如果将量词更改为+
,则可以确定字符串中的字符超过1个,这将消除您的默认$min
要求(尽管错误消息的具体程度较低)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.