简体   繁体   English

HTML 输入模式属性

[英]HTML input pattern attribute

I'm want to take an input from user that is of the form "Z1C19999".我想从用户那里获取“Z1C19999”形式的输入。 So how i do set the pattern attribute.那么我如何设置模式属性。

I have tried using the following code.我尝试使用以下代码。

<input type="text" name="farmercode" class="text" pattern="Z[1-3]+C[1-5]+[0-9]{5,8}" title="* Farmer's Lab Code can have only alphabets Z, C and digits between 0 and 9 in the format "Z1C19999" ">

The input must have the syntax Z[Any value between 1 to 3]C[Any value between 1 to 5][Any value between 0 to 9999].输入必须具有语法 Z[1 到 3 之间的任何值]C[1 到 5 之间的任何值][0 到 9999 之间的任何值]。 The minimum length is 5 and max 8.最小长度为 5,最大长度为 8。

In my case even if I give 123456 only as input it is still accepting the value which is wrong.就我而言,即使我仅将 123456 作为输入,它仍然接受错误的值。 I want it to start with Z.我希望它以 Z 开头。

Your pattern works great in Chrome where I tested: see this CodePen您的模式在我测试的 Chrome 中效果很好:请参阅此 CodePen

 input[pattern] ~ span { display: none; } input[pattern]:valid ~ span.valid { display: inline-block; } input[pattern]:invalid ~ span.invalid { display: inline-block; }
 <form> <input type="text" name="farmercode" class="text" pattern="Z[1-3]+C[1-5]+[0-9]{5,8}" title="* Farmer's Lab Code can have only alphabets Z, C and digits between 0 and 9 in the format Z1C19999"> <span class="valid">Valid</span> <span class="invalid">Invalid</span> </form>

However, I think the RegExp that best describes your sentence is as follow但是,我认为最能描述您的句子的 RegExp 如下

The input must have the syntax Z[Any value between 1 to 3]C[Any value between 1 to 5][Any value between 0 to 9999].输入必须具有语法 Z[1 到 3 之间的任何值]C[1 到 5 之间的任何值][0 到 9999 之间的任何值]。 The minimum length is 5 and max 8.最小长度为 5,最大长度为 8。

Z[1-3]C[1-5][0-9]{5,8}

Which leads me to the conclusion: What makes you think it doesn't work?这使我得出结论:是什么让您认为它不起作用?

TIP: Add the required attribute to force something inside.提示:添加required属性以强制内部的某些内容。

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

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