开发者

Dropdown box / radio button validation?

开发者 https://www.devze.com 2023-02-04 08:11 出处:网络
I\'m using the jQuery tools (http://flowplayer.org/tools/demos/index.html) validator for my form and was wonder开发者_如何学运维ing how to validate dropdowns and radio buttons?

I'm using the jQuery tools (http://flowplayer.org/tools/demos/index.html) validator for my form and was wonder开发者_如何学运维ing how to validate dropdowns and radio buttons?

Anyone had experience of implementing it?


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>


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;
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号