过去我已经为 JQuery 验证插件组合了一些自定义方法,但这是我想要检查多个条件的第一个方法。
这是一个简单的场景,我试图检测字段中是否存在 html。我不需要广泛的正则表达式,我只想告诉用户 html 以客户端方式允许。如果字段值中有 html,我当然会阻止服务器端提交,但我想要一个好的用户体验。
必须有一种更简单的方法来查找一些关键元素,可以通过遍历数组来完成吗?
<script language="javascript">
$(document).ready(function(){
$.validator.addMethod("findhtml", function(value, element) {
var commentclean = new Boolean();
if (element.value.indexOf("<") != -1)
{
commentclean= false;
//alert('found <');
}
else if
(element.value.indexOf("http") != -1){
commentclean= false;
//alert('found http');
}
else
{
commentclean= true;
//alert('nothing found')
}
return this.optional(element) || ( commentclean );
}, "* match the string");
});
</script>