2011-01-12 92 views
0

我使用jQuery工具(http://flowplayer.org/tools/demos/index.html)驗證器爲我的表單,並想知道如何驗證下拉菜單和單選按鈕?下拉框/單選按鈕驗證?

任何人都有實施它的經驗?

+0

有一個明顯的例子爲驗證[文檔】在一個複選框[(http://flowplayer.org/tools/demos/validator/events .html)你到目前爲止做了什麼向我們展示你的結果(代碼)以更好地幫助你 – ifaour 2011-01-12 13:36:19

+0

對不起,錯字,不希望複選框...需要它在下拉和單選按鈕上工作! – 2011-01-12 13:39:17

回答

0

在文檔中說它也可以對SELECT元素使用required =「required」屬性,但它對我也不適用。 我選擇使用自定義驗證器功能。希望這對你有用。這是非常基本的,但它缺少一些其他考慮因素使其更加靈活。

$.tools.validator.fn("select[required=required]", function(input, value) { 
    // If the first item in the list is selected return FALSE 
    return !input[0].options[0].selected; 
}); 
1

只需添加它像這樣:

<script type="text/javascript"> 
$(document).ready(function(){ 
    // initialize validator and supply the onBeforeValidate event in configuration 

$("#GetAQuote").validator({ 
    position: 'top left', 
    offset: [-5, 25], 
    message: '<div><em/><img src="images/form-error.gif" style="float:right;"/></div>'  
    // em element is the arrow 
}); 

$.tools.validator.fn("select[required=required]", function(input, value) { 
    // If the first item in the list is selected return FALSE 
    return !input[0].options[0].selected; 
}); 


$("#GetAQuote").bind("onFail", function(e, errors) { 

// we are only doing stuff when the form is submitted 
if (e.originalEvent.type == 'submit') { 

    // loop through Error objects and add the border color 
    $.each(errors, function() { 
     var input = this.input; 
     input.css({borderColor: '#e6a200'}).focus(function() { 
      input.css({borderColor: '#75a9cc'}); 
     }); 
    }); 
    } 
    }); 
}); 
</script>