我必须输入允许空格的28个字符的验证,如果我们输入任何超过28个字符的名字,它应该拒绝那个 [英] I have to enter a validation for 28 character which allows white space and if we enter any name having more then 28 char, it should rather reject that

查看:72
本文介绍了我必须输入允许空格的28个字符的验证,如果我们输入任何超过28个字符的名字,它应该拒绝那个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[A-Za-z0-9 _ @。/#& + - \ / s] {0,28}/>

但这允许空格但不会t拒绝拥有超过28个字符的名称,而不是截断它,并且只允许28个字符。



我尝试过:



[A-Za-z0-9 _ @。/#& + - \ / s] {0,28}/>

解决方案

您的模式中存在一些错误。



- 减号定义括号内的字符类中的范围。因此,如果要匹配角色,则必须进行转义或第一个角色。当意图使用 + - \ / 时它会匹配从0x2b到0x2f的所有字符(包括可以删除的点)。



最后 s 是没用的。



To允许空格必须 \s

[/ EDIT]



要检查有效长度,您必须使用 ^






您尚未指定使用的语言。这是一个有效的Perl正则表达式:

  编辑:带有空格 
范围从+到/
if(


test =〜/ ^ [A-Za-z0- 9 _ @#&安培; + - \ / \s] {0,28}

"[A-Za-z0-9_@./#&+-\/s]{0,28}"/>
but this allows white space but doesn't reject name having more then 28 char rather it truncates that and allows only 28 char for the same .

What I have tried:

"[A-Za-z0-9_@./#&+-\/s]{0,28}"/>

解决方案

There are some mistakes in your pattern.

The - minus sign defines a range within a bracketed character class. So it has to be escaped or the first character if you want to match the character. When using +-\/ by intention then it matches all characters from 0x2b to 0x2f (which includes the dot which can then be removed).

The s at the end is useless.
[EDIT]
To allow white spaces it must be \s.
[/EDIT]

To check for valid lengths you have to match the whole string using ^ and


.

You have not specified which language you are using. Here is a working Perl regex:

# Edit: With white spaces
# With range from + to /
#if (


test =~ /^[A-Za-z0-9_@#&+-\/\s]{0,28}


这篇关于我必须输入允许空格的28个字符的验证,如果我们输入任何超过28个字符的名字,它应该拒绝那个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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