我有一个多行文本框,我不应该允许用户输入任何 HREF 链接。所以我正在尝试使用正则表达式进行字符串匹配,如下所示。但我没有收到错误消息。我究竟做错了什么?
Regex strMatch = new Regex(@"^(?:(?:<a )?href|</a>)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (strMatch.IsMatch(EmailTextBox.Text.Trim()))
{
vsumPage.AddErrorMessage("Cannot enter links");
}
同样的检查也需要在 JS 中工作。但它给了我一些例外。不知道我做错了什么。这是我的 JS 函数
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);
}
}
这是我正在测试它的输出
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>
再次更新:尝试了这个,如下所示:但它总是出现在警报“Test3 0”部分......即使是
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;
}
}