PHP验证文本区域 [英] PHP validation of a textarea

查看:103
本文介绍了PHP验证文本区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试验证用户在我的网站上提供的信息.

我有一个文本区域来存储某些内容的描述.
我想知道如何检查用户是否仅输入数字,字母和常用标点字母.

这就是我检查输入的电子邮件地址是否有效的方式.

Hi, I am trying to validate the information provided by the user on my website.

I have a textarea to store description of something.
I want to know how can I check if the user only enters numbers, alphabets and common punctuation letters only.

This is how I am checking if the email address entered is valid.

//checks if the email address is a valid email address
              if(!filter_var($_POST['contactInfo'], FILTER_VALIDATE_EMAIL))

推荐答案

_POST [' contactInfo'],FILTER_VALIDATE_EMAIL))


使用正则表达式.

Use a regular expression.

if (preg_match(/^[A-Za-z0-9.,+]/,


_POST[''your_field'']) == 0) // text is good
_POST[''your_field'']) == 0) // text is good



阅读正则表达式,尤其是字符类和转义.将您要接受的字符放在[]内.在我的示例中,可以接受大小写字母,数字,句点逗号和加号.

如果您无法使用它,可以使用在线正则表达式工具.

干杯,
彼得
如果这回答了您的问题,请将其标记为已接受.仍要投票.



Read up on regular expressions, particularly character classes and escaping. Put the characters you will accept inside the []. In my example, upper- and lower case letters, digits, period comma and plus are acceptable.

If you''re having trouble getting it to work, there are online regex tools you can use.

Cheers,
Peter
If this answers your question, mark it accepted. Vote anyway.


这篇关于PHP验证文本区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆