Search code examples
javascriptbindingalerthtml-escape-characters

How to pass single quote inside a '<%# %>' quantifier


I have a neat Javascript function that checks the validity of a bound field. However, I want to place an ELSE {} in the line to send an alert() function call and display an error.

This is what works for me...

onClick='<%# "if(chkMobile(\"" + Eval("pssPhone").ToString() + "\")) { highlightRow(this); }" %>'

This does not work for me ...

onClick='<%# "if(chkMobile(\"" + Eval("pssPhone").ToString() + "\")) { highlightRow(this); } else { alert(&apos;Invalid mobile phone number&apos;); }" %>'

I've tried to escape the single quotes with \', \\\', \''', and even ' on its own. I always get badly formed errors.

The &apos; does not work either, but at least I don't get any errors. In fact with the &apos; the highlightRow() doesn't fire which is worse!

I've also tried &quot; with no luck.

Thank you for your time.


Solution

  • [SOLVED] I took a punt and decided to inject double-quotes and it works like a charm!

    onClick='<%# "if(chkMobile(\"" + Eval("pssPhone").ToString() + "\")) { highlightRow(this); } else { alert(\"" + "Invalid mobile phone number" + "\"); }" %>'