简体   繁体   English

在我的代码中检查错误以验证 php 中的字母数字和空格

[英]Checking error in my code for validate letters numbers and whitespace in php

I made a form for submit name of the product website in php.我在php中制作了一个提交产品网站名称的表格。 I wrote simple code that matches only letters, numbers and whitespace but no special characters.我编写了仅匹配字母、数字和空格但不匹配特殊字符的简单代码。 Its working fine for me.它对我来说工作正常。

I tried this我试过这个

if (!preg_match("/^[a-zA-Z0-9 ]*$/",$name))

Could you please found any error or any principle of writing the correct expression that i forgot here in my code .?您能否在我的代码中找到任何错误或编写我忘记的正确表达式的任何原则。?

In the comments, you mention that is must match a space after the first leter or number So you should modify your preg_match like so:在评论中,您提到必须第一个字母或数字之后匹配一个空格所以您应该像这样修改您的 preg_match :

preg_match("/^[a-zA-Z0-9][a-zA-Z0-9 \t]*$/",$name)

This will match the fist letter or number, then any number of letters, numbers, space and tab.这将匹配第一个字母或数字,然后是任意数量的字母、数字、空格和制表符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM