[英]Regular Expression to avoid HTML tags and empty values
I have applied a textbox click validation and wanted to avoid any html tags in text box also the simple < (open tag) and >(close tag). 我已经应用了文本框单击验证,并希望避免在文本框中使用任何html标签,也不要使用简单的<(打开标签)和>(关闭标签)。 The below code is working for but i want to add additional validations also for empty strings and other tags in html.
下面的代码适用于我,但我想为html中的空字符串和其他标签添加其他验证。 Can some one please help modify the regex for the requirement.
有人可以帮忙修改要求的正则表达式吗?
function htmlValidation()
{
var re = /(<([^>]+)>)/gi;
if (document.getElementById(’<%=TextBox2.ClientID%>’).value.match(re)){ document.getElementById(’<%=TextBox2.ClientID%>’).value = “”;
return false;
}
return true;
}
Corrected Code above 上面的更正代码
In my opinion, I believe you'll have a good hard work if you want to validate such things. 我认为,如果您想验证这些事情,那么您会付出艰辛的努力。
Instead of preventing HTML content in a text box, other solution could be just html entity encode Text property, so <p>a</p>
would be converted to >p<a>p<
除了防止文本框中的HTML内容外,其他解决方案也可以只是html实体编码的Text属性,因此
<p>a</p>
将被转换为>p<a>p<
. 。
Result of that is you're going to render the HTML "as text" instead of getting it interpreted by Web browser. 结果是您将“以文本形式”呈现HTML,而不是通过Web浏览器对其进行解释。
Check this MSDN article: 检查此MSDN文章:
$("#<%= btnAdd.ClientID %>").click(function () {
var txt = $("#<%= txtBox1.ClientID %>");
var svc = $(txt).val(); //Its Let you know the textbox's value
var re = /(<([^>]+)>)/gi;
if(txt.val()!=""){
if (!txt.val().match(re)) {
//my Operations
//goes here
});
return false;
}
else {
alert("Invalid Content");
}
}
else {
alert("Blank value selected");
}
I have used Jquery function to check for regular expresion. 我已经使用Jquery函数检查常规表达式。 This question is a linked question with Using Jquery to add items in Listbox from Textbox
此问题是与使用Jquery从文本框中添加列表框中的项目的链接问题
Now i can mark this as my final answer. 现在,我可以将其标记为我的最终答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.