[英]HTML text input RegEx validation not working even with required attribute
I am trying to have my dynamically generated text inputs only allow for digits and an optional decimal point in between. 我试图让我动态生成的文本输入仅允许数字和介于两者之间的可选小数点。 I am using the attribute but the inputs are still unresponsive to the RegEx. 我正在使用该属性,但是输入仍然无法响应RegEx。
var howMuch = $("<input>").attr("type", "text").attr("name", "howMuch").attr("pattern", "([0-9]+(\.[0-9]+)?)").prop("required", true);
The HTML generates like so: HTML HTML生成如下: HTML
PLEASE do not mark this question as duplicate, none of the existing similar answers are already using the 'required' attribute 请不要将此问题标记为重复,没有类似的答案已经在使用'required'属性
Pointy answer applies. 尖锐的答案适用。 Also, pattern
attributes only works at form validation time. 此外, pattern
属性仅在表单验证时起作用。 In other words, user is able to type in whatever he wants. 换句话说,用户可以键入他想要的任何内容。 Only when he presses submit, pattern
attribute is taken into consideration. 仅当他按下提交时,才会考虑pattern
属性。 If you want a realtime feedback to user you should listen to change
events and manually trigger the form validate
method. 如果您想向用户提供实时反馈,则应收听change
事件并手动触发表单validate
方法。 Or you can use the keydown
event to prevent some characters to be accepted as input. 或者,您可以使用keydown
事件来阻止某些字符被接受为输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.