使用正则表达式验证表单数据 [英] Validating form data using regex

查看:140
本文介绍了使用正则表达式验证表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是最新和高兴能在这里在Stackoverflow。我很长时间以来一直关注这个网站。



我有一个输入/文本字段。 (例如插入你的名字)



脚本在用户发送数据时启动以下控制:



1)控制字段是否为空。



2)控制字段是否超过允许的最大字符数。

3)控制该字段是否包含错误匹配的preg_match正则表达式。 (例如,它包含数字而不是字母 - 没有符号和口音)。

问题是:为什么如果我把这个字符w /,脚本不能控制?它看起来像字符串旁路控制?






所有人都好,如果我迟到与答案(也为非代码张贴^^)。

现在,谈论我的问题。我检查了只有脱机工作时才会出现问题(我使用EasyPhp 5.3.6.1)。



这是我用来获取上述内容的代码:

  if(!preg_match('/ ^ [a-zA-Z] + []?[a-zA-Z] + $ /',$ name)){

回显错误;






正如你所看到的,这段代码匹配:




  • 仅以字母开头(和结束)的字符串;
  • 仅含0或1个空字符串空间(对于有两个名字的人,即Anna Maria);


...正确!!



(请纠正我的错误)



感谢所有!



Wart

解决方案

您也可以试试这个:

<$ p $ (preg_match('/ ^ [a-z0-9] {1,12} / im',$ subject)){
\\\ match
}

以上只会匹配类似于@genesis'的帖子,但只会在$ subject为介于1至12个字符之间。正则表达式也不区分大小写。


I'm newest and happy to be here in Stackoverflow. I am following this website since a long time.

I have an input/text field. (e.g. "Insert your name")

The script starts the following controls when the user sends data:

1) Control whether the field is empty or not.

2) Control whether the field goes over maximum characters allowed.

3) Control whether the field contain a wrong matched preg_match regex. (e.g. it contains numbers instead of only letters - without symbols and accents -).

The question is: why if i put this characters "w/", the script doesn't make the control? And it seems like the string bypass controls?


Hello to all guys and sorry if I'm late with the answer (and also for the non-code posted ^^).

Now, talking about my problem. I checked that the problem is on ONLY if I work offline (I use EasyPhp 5.3.6.1). Otherwise the regEx tested online is ok.

This is the code I use to obtain only what I said above:

    if (!preg_match('/^[a-zA-Z]+[ ]?[a-zA-Z]+$/', $name)) {

         echo "Error";

    }

As you can see, this code match:

  • A string that start (and finish) with only letters;
  • A string with only 0 or 1 empty space (for persons who has two name, i.e.: Anna Maria);

...right?!

(Please correct me if I am wrong)

Thanks to all!

Wart

解决方案

You can also try this:

if (preg_match('/^[a-z0-9]{1,12}/im', $subject)) {
    \\ match
}

The above will only match similar to @genesis' post, but will only match if the $subject is between and including 1 - 12 characters long. The regex is also case insensitive.

这篇关于使用正则表达式验证表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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