英文:
JS Regex Pattern Used In Data-XXX Attributes Not Working
问题
以下是翻译好的部分:
"i am trying to set regex pattern in input field attribute and use js to validate it.. but somehow it's not working.."
我正在尝试在输入字段属性中设置正则表达式模式,并使用 JavaScript 进行验证,但不知何故它不起作用。
"if i use same pattern directly on js it works.. but not through the attribute.."
如果我直接在 JavaScript 中使用相同的模式,它可以工作,但通过属性设置就不行。
"here is my html code:"
以下是我的 HTML 代码:
"<input type="text" name="tPhoneNumber" id="tPhoneNumber" style="width:90%;" data-name="Phone Number" data-group="NewEntry" data-required="y" data-pattern="/^+44[\d]{10}$/" />"
"and here is the js code:"
以下是 JavaScript 代码:
"//this works
if (/^+44[\d]{10}$/.test(inputs[i].value))
{
console.log("matched");
}
else
{
console.log("not matched");
}
//this does not works, it's always failed regardless whether the text box has correct value or not
if(!inputs[i].value.match(inputs[i].dataset.pattern))
{
var msg = Invalid data entered in \"${inputs[i].dataset.name}\" field!<br/>You have entered = ${inputs[i].value}
;
return ShowError(msg);
}"
"what i am doing wrong here?"
我在这里做错了什么?
"thanks in advance"
提前感谢。
英文:
i am trying to set regex pattern in input field attribute and use js to validate it.. but somehow it's not working..
if i use same pattern directly on js it works.. but not through the attribute..
here is my html code:
<input type="text" name="tPhoneNumber" id="tPhoneNumber" style="width:90%;" data-name="Phone Number" data-group="NewEntry" data-required="y" data-pattern="/^\+44[\d]{10}$/" />
and here is the js code:
//this works
if (/^\+44[\d]{10}$/.test(inputs[i].value))
{
console.log("matched");
}
else
{
console.log("not matched");
}
//this does not works, it's always failed regardless whether the text box has correct value or not
if(!inputs[i].value.match(inputs[i].dataset.pattern))
{
var msg = `Invalid data entered in "${inputs[i].dataset.name}" field!<br/>You have entered = ${inputs[i].value}`;
return ShowError(msg);
}
what i am doing wrong here?
thanks in advance
best regards
答案1
得分: 2
由于你输入的数据属性仅是字符串,而不是 RegExp
对象,你应该移除其值的开头和结尾的斜杠 /
:data-pattern="^\+44[\d]{10}$"
。
var input = document.getElementById('tPhoneNumber');
// 这个有效
if (/^\+44[\d]{10}$/.test(input.value))
{
console.log("匹配");
}
else
{
console.log("不匹配");
}
// 这个也应该有效
if(!input.value.match(input.dataset.pattern))
{
console.log(`在“${input.dataset.name}”字段中输入的数据无效!<br/>您输入的内容为 = ${input.value}`);
}
<input type="text" name="tPhoneNumber" id="tPhoneNumber" value="+440123456" style="width:90%;" data-name="Phone Number" data-group="NewEntry" data-required="y" data-pattern="^\+44[\d]{10}" />
英文:
Since data attribute inside your input is just string, not a RegExp
object, you should remove slashes /
at start and end of its value: data-pattern="^\+44[\d]{10}$"
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var input = document.getElementById('tPhoneNumber');
//this works
if (/^\+44[\d]{10}$/.test(input.value))
{
console.log("matched");
}
else
{
console.log("not matched");
}
//this should works too
if(!input.value.match(input.dataset.pattern))
{
console.log(`Invalid data entered in "${input.dataset.name}" field!<br/>You have entered = ${input.value}`);
}
<!-- language: lang-html -->
<input type="text" name="tPhoneNumber" id="tPhoneNumber" value="+440123456" style="width:90%;" data-name="Phone Number" data-group="NewEntry" data-required="y" data-pattern="^\+44[\d]{10}$" />
<!-- end snippet -->
答案2
得分: 2
你的代码中的正则表达式之所以有效,是因为你使用了正则表达式字面量,它创建了一个RegExp
的实例。正则表达式字面量是一个放置在斜杠之间的正则表达式(可选地跟随标志)。
你的属性模式不起作用,因为自定义数据属性在JavaScript中表示为字符串4。
所以当你调用.match(dataset.pattern)
时,你传递的是一个字符串,而不是一个RegExp
对象。这个字符串会转换为RegExp
对象。
前导和尾随的斜杠是用于创建RegExp
对象的JS语法,因此data-pattern
和你的JS RegExp
不是相同的。
data-pattern
应该只表示正则表达式,以便正确转换它;移除data-pattern
的前斜杠。
英文:
Your in-code regex works because you are using a regular expression literal, which creates an instance of RegExp
. A regular expression literal is a regular expression placed between forward-slashes (optionally followed by flags).
Your attribute pattern does not work because custom data attributes are represented as strings in JavaScript.
So when you call .match(dataset.pattern)
, you pass a string instead of a RegExp
object. The string converts to a RegExp
object.
The leading and trailing forward-slashes are JS syntax for creating a RegExp
object, so data-pattern
and your JS RegExp
are not the same.
data-pattern
should only represent the regular expression, so that it will be converted correctly; remove the forward-slashes of data-pattern
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论