Dropdown box / radio button validation?

混江龙づ霸主 提交于 2019-12-08 10:56:18

问题


I'm using the jQuery tools (http://flowplayer.org/tools/demos/index.html) validator for my form and was wondering how to validate dropdowns and radio buttons?

Anyone had experience of implementing it?


回答1:


In the documentation it says you can also use the required="required" attribute for SELECT elements, but it's not working for me either. I opted to use the custom validator function. Hope this works for you. It is pretty basic though, its missing some other considerations to make it more flexible.

$.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;
});



回答2:


Just add it like so:

<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>


来源:https://stackoverflow.com/questions/4669106/dropdown-box-radio-button-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!