简体   繁体   English

HTML文本输入RegEx验证即使具有必需的属性也不起作用

[英]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.

相关问题 尝试使用 JavaScript 在文本文件中保存表单数据时,HTML 输入必需属性不起作用 - HTML input required attribute not working when trying to Save form Data in a Text File using JavaScript 在JavaScript中提交验证之前,HTML5“required”属性不起作用 - HTML5 “required” attribute not working before submit validation in JavaScript 正则表达式验证适用于 JavaScript 但不适用于 HTML5 输入验证模式 - Regex validation working with JavaScript but not with HTML5 input validation pattern 输入类型为图像时,HTMl5必需属性不起作用 - HTMl5 required attribute not working when input type is image 输入的JavaScript必需验证无效 - JavaScript Required Validation on Input not working 所需的 HTML Bootstrap 属性不起作用 - HTML Bootstrap attribute required not working 应用选择菜单的表单验证类似于的html5必需属性<input> - Applying form validation of select menu similar to html5 required attribute of <input> JavaScript文本验证不使用正则表达式 - JavaScript Text Validation Not Working with Regex jQuery验证-文本输入所需的单选按钮 - JQuery Validation - radio button required for text input 如何使必需属性对 HTML 上的输入起作用 - How to Make a Required attribute work on an Input on HTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM