Dropdown box / radio button validation?
Asked Answered
T

2

0

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?

Textual answered 12/1, 2011 at 13:6 Comment(2)
There are a clear example for validating a checkbox in the [documentation][(flowplayer.org/tools/demos/validator/events.html) what have you done so far show us your results (code) to better help youMcwhorter
Sorry, typo, didn't want checkboxes... need it to work on dropdowns and radio buttons!Textual
C
0

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;
});
Chamonix answered 13/1, 2011 at 18:52 Comment(0)
A
1

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>
Alcestis answered 19/7, 2012 at 16:11 Comment(0)
C
0

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;
});
Chamonix answered 13/1, 2011 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.