我在過去爲JQuery驗證插件放了一些自定義方法,但這是我想要檢查多個條件的第一個。是否有一種更優雅,可擴展的方式將多個條件添加到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>
你應該使用正則表達式來尋找html標記。例如,字符串「0 <1」不包含html。在clien方面使用正則表達式並不像你猜想的那麼昂貴:) – YD1m