最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Dynamically adding pattern and title attributes to input control - Stack Overflow

programmeradmin1浏览0评论

I have a HTML input control as follows:

<input type="text" value="<%= this.CustomerAcctNumber %>" name="CustomerAcctNumber" id="CustomerAcctNumber" maxlength="19" onkeyup="CustomerAcctNumberChange()" required >

on body onload I am adding a pattern and title attributes to this input control

var CustomerAcctNumber = document.getElementById("CustomerAcctNumber");
CustomerAcctNumber.setAttribute("pattern","\d{2}-(?:\d{4}-){3}\d{1}");
CustomerAcctNumber.setAttribute("title","xx-xxxx-xxxx-xxxx-x");

When i submit this web page, with valid pattern, it gives me error :

Input control was rendered as expected as follows :

<input type="text" value="" name="CustomerAcctNumber" id="CustomerAcctNumber" maxlength="19" onkeyup="CustomerAcctNumberChange()" pattern="d{2}-(?:d{4}-){3}d{1}" title="xx-xxxx-xxxx-xxxx-x" required="">

Any suggestions or ments will be appreciated!!

I have a HTML input control as follows:

<input type="text" value="<%= this.CustomerAcctNumber %>" name="CustomerAcctNumber" id="CustomerAcctNumber" maxlength="19" onkeyup="CustomerAcctNumberChange()" required >

on body onload I am adding a pattern and title attributes to this input control

var CustomerAcctNumber = document.getElementById("CustomerAcctNumber");
CustomerAcctNumber.setAttribute("pattern","\d{2}-(?:\d{4}-){3}\d{1}");
CustomerAcctNumber.setAttribute("title","xx-xxxx-xxxx-xxxx-x");

When i submit this web page, with valid pattern, it gives me error :

Input control was rendered as expected as follows :

<input type="text" value="" name="CustomerAcctNumber" id="CustomerAcctNumber" maxlength="19" onkeyup="CustomerAcctNumberChange()" pattern="d{2}-(?:d{4}-){3}d{1}" title="xx-xxxx-xxxx-xxxx-x" required="">

Any suggestions or ments will be appreciated!!

Share Improve this question edited Sep 20, 2016 at 11:25 Mandar Patil asked Sep 20, 2016 at 10:53 Mandar PatilMandar Patil 5382 gold badges10 silver badges30 bronze badges 5
  • is the pattern & title valued rendered correctly in HTML as you want ? – Nikhil Nanjappa Commented Sep 20, 2016 at 11:18
  • the first line var CustomerAcctNumber = document.getElementById(" is abrupt. can you add that entire line in here? – Nikhil Nanjappa Commented Sep 20, 2016 at 11:21
  • I have edited my question to correct the first line. – Mandar Patil Commented Sep 20, 2016 at 11:26
  • Yes pattern and title is rendered as expected. – Mandar Patil Commented Sep 20, 2016 at 11:26
  • The pattern doesn't look right to me for what you need. have you tested it elsewhere ? – Nikhil Nanjappa Commented Sep 20, 2016 at 12:05
Add a ment  | 

1 Answer 1

Reset to default 3

I figured out the problem. You are passing the pattern value from JavaScript, so it escapes all your \ symbols which is needed to be generated in HTML. currently you're HTML generates pattern="d{2}-(?:d{4}-){3}d{1}" which is incorrect. So you need to provide another escape character next to each of the existing \ which will result to double \\ so you're HTML generates pattern="\d{2}-(?:\d{4}-){3}\d{1}". So the line in your JS would bee as shown below:

CustomerAcctNumber.setAttribute("pattern","\\d{2}-(?:\\d{4}-){3}\\d{1}"); //renders to '\d{2}-(?:\d{4}-){3}\d{1}' in HTML

Rest all are fine. Let me know if this worked.

发布评论

评论列表(0)

  1. 暂无评论