Html HTML5 表单模式/验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17353792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:33:24  来源:igfitidea点击:

HTML5 Form Pattern / Validation

htmlvalidationhtml5-validation

提问by Robert

I am trying to come up with a valid pattern for an HTML5 form. I would like to allow all upper & lower case letters, spaces, numbers, and the symbols / \ @ # $ % & .

我正在尝试为 HTML5 表单提出有效的模式。我想允许所有大写和小写字母、空格、数字和符号 / \ @ # $ % & 。

I've tried a couple different formats but I haven't had any luck.

我尝试了几种不同的格式,但我没有任何运气。

So far I have:

到目前为止,我有:

Address: <input type="text" name="Address" size="25"  pattern="[a-zA-Z0-9 ]{2,35}]" title="Title" required/>

回答by Alexander Jardim

This is an example I built with w3schools test page:

这是我使用 w3schools 测试页构建的示例:

<!DOCTYPE html>
<html>
<body>

<form onsubmit="getElementById('s').innerHTML=getElementById('i').value;return false;">
  Country code: <input id="i" type="text" name="country_code" pattern="([a-zA-Z0-9]| |/|\|@|#|$|%|&)+" title="Three letter country code">
  <input type="submit">
</form>
<p>Submitted value: <span id="s">

Note: The pattern attribute of the input tag is not supported in Internet Explorer 9 and earlier versions, or in Safari.

注意:Internet Explorer 9 及更早版本或 Safari 不支持 input 标签的模式属性。

You can use it to make your own tests at http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_pattern

您可以使用它在http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_pattern上进行自己的测试