简体   繁体   English

HTML5正则表达式模式验证

[英]HTML5 Regex Pattern Validation

The validation I need is the following AZ, az, 0-9 , no trailing/leading whitespace, -._ only once non-trailing/leading 我需要的验证是以下AZ, az, 0-9 ,无结尾/前导空格, -._仅一次非结尾/前导

Till now I have the following: 到现在为止,我有以下内容:

<input type="text" pattern="^\S[a-zA-Z0-9]*\S$">

This takes care of the leading trailing whitespace and AZ,az,0-9, but it needs minimum 2 characters 这会照顾到前导尾随空格和AZ,az,0-9,但至少需要2个字符

Then I thought of something like this for the symbols -._ 然后我想到了这样的符号-._

<input type="text" pattern="^\S[a-zA-Z0-9]*[\-_+]{,1}\S$">

But no luck there. 但是那里没有运气。 Any ideas? 有任何想法吗?

Since pattern attribute uses the same syntax as JavaScript, you can use the following regex: 由于pattern属性使用与JavaScript相同的语法,因此可以使用以下正则表达式:

 <input type="text" pattern="^(?=..)[a-zA-Z0-9]*([_.-][a-zA-Z0-9]*)?$"> 

You can test with the snippet above by typing something in, then change focus from the input. 您可以通过输入一些内容来测试上面的代码段,然后从输入中更改焦点。

The input can only contain a-zA-Z0-9 and at most once of _.- . 输入只能包含a-zA-Z0-9最多一次的_.- The look-ahead in front (?=..) checks that there are at least 2 characters. 前面的前瞻(?=..)检查是否至少有2个字符。

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

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