I have a multiline textbox in which I should not allow the user to enter any HREF links.
So I am trying to string match using Regex as follows.
But I am not getting the error message. What am I doing wrong?
Regex strMatch = new Regex(@"^(?:(?:<a )?href|</a>)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (strMatch.IsMatch(EmailTextBox.Text.Trim()))
{
vsumPage.AddErrorMessage("Cannot enter links");
}
Also the same check needs to work in JS too. But it it giving me some exception. not sure what am i doing wrong. Here is my JS function
function doPreview()
{
var getEncodedStr = encode("<%=EmailTextBox.ClientID%>");
var strInvalid = "Email instructions cannot have links included";
var str = getEncodedStr.match(/@"(?:<a )?href|</a>"/) ;
if (str == null)
{
var targetURL = 'Preview.aspx?txtName=' + getEncodedStr ;
window.open(targetURL, null, "height=700,width=600,dependent=yes,hotkey=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no");
return false;
}
else
{
alert(strInvalid);
}
}
Here’s the output that I am testing it with
Testing Testing <br/>
<b> Hello Testing </b> <br/>
<b> Testing 2000 character and link check </b> <br/>
<b> Hello </b> <a href="www.google.com">Click Me </a>
Updated again:
Tried this one as given below:
But it always comes to the alert “Test3 0” part…even when an
function doPreview()
{
var getEncodedStr = encode("<%=EmailTextBox.ClientID%>");
var strInvalid = "Email instructions cannot have links included";
var checkCount = "0" ;
alert("Test 1" + checkCount);
if(/<\/[\s\S]?a\b/gm.test(decode(getEncodedStr)))
{
checkCount = "1" ;
alert("Test 2" + checkCount);
alert((/<\/?a\b/.test(decode(getEncodedStr))));
return false;
}
if (checkCount == "0")
{
alert("Test 3" + checkCount);
var targetURL = 'EPreview.aspx?txtName=' + getEncodedStr ;
window.open(targetURL, null, "height=700,width=600,dependent=yes,hotkey=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=no,toolbar=no");
return false;
}
}
The
^asserts that this pattern occurs at the beginning of the string. Try removing it. Also you could probably change the pattern to:This will simply look for a
<aor</amaking sure that it is really an anchor tag, by using the word-boundary\b(which asserts that there is no letter or digit or underscore afterwards – but a space or>for example).If that does not do it, it would really help to see your actual input.