JS正则表达式模式在Data-XXX属性中使用时不起作用

huangapple go评论81阅读模式
英文:

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:

&lt;input type=&quot;text&quot; name=&quot;tPhoneNumber&quot; id=&quot;tPhoneNumber&quot; style=&quot;width:90%;&quot; data-name=&quot;Phone Number&quot; data-group=&quot;NewEntry&quot; data-required=&quot;y&quot; data-pattern=&quot;/^\+44[\d]{10}$/&quot; /&gt;

and here is the js code:

//this works
if (/^\+44[\d]{10}$/.test(inputs[i].value))
{
    console.log(&quot;matched&quot;);
}
else
{
    console.log(&quot;not matched&quot;);
}
//this does not works, it&#39;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 &quot;${inputs[i].dataset.name}&quot; field!&lt;br/&gt;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=&quot;^\+44[\d]{10}$&quot;

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=&quot;^\+44[\d]{10}$&quot;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var input = document.getElementById(&#39;tPhoneNumber&#39;);
//this works
if (/^\+44[\d]{10}$/.test(input.value))
{
    console.log(&quot;matched&quot;);
}
else
{
    console.log(&quot;not matched&quot;);
}

//this should works too
if(!input.value.match(input.dataset.pattern))
{
    console.log(`Invalid data entered in &quot;${input.dataset.name}&quot; field!&lt;br/&gt;You have entered = ${input.value}`);
}

<!-- language: lang-html -->

&lt;input type=&quot;text&quot; name=&quot;tPhoneNumber&quot; id=&quot;tPhoneNumber&quot; value=&quot;+440123456&quot; style=&quot;width:90%;&quot; data-name=&quot;Phone Number&quot; data-group=&quot;NewEntry&quot; data-required=&quot;y&quot; data-pattern=&quot;^\+44[\d]{10}$&quot; /&gt;

<!-- 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.

huangapple
  • 本文由 发表于 2023年2月18日 15:04:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定