简体   繁体   中英

jQuery validation plugin checks drop-down, scientific, and elements with similar names

This post is based on my previous question . I had a form with three sections, and use back and next button to hide and show users only one table section at a time. In addition, the next button also serves as validation trigger.

My question is:

  1. My validation approach failed to check the drop down field.
  2. Is there an express way in jQuery validation plugin that I can validate fields with similar names? For example, I have fields with name mm1 , mm2 , and mm3 . Can use some shortcut way such as mm* in the validation rule?
  3. I found the build-in method number could not validate scientific inputs. Is there other build-in method to deal with it? Do I have to add my own method for this?

Thanks for any inputs! Here is a DEMO .

HTML

<form method="post" id="form1" action=index.html>
    <table>
         <H4 align="center" id="id_tab">
            |<a href="#" class="Chemical"> Chemical </a>|
             <a href="#" class="Application"> Application </a>|
             <a href="#" class="Physical"> Physical </a>|
            </H4>
    </table><br>
    <table class="tab tab_Chemical" border="0">
        <tr><th><label for="id_wat_hl">Water Column Half life (days):</label></th>
            <td><input type="text" name="wat_hl" id="id_wat_hl" value="1e-08" /></td></tr>
    </table>
    <table class="tab tab_Application" border="0" style="display:none">
        <tr><th scope="col"><label for="id_noa">Number of Applications:</label></th>
             <td scope="col"><select name="noa" id="id_noa">
                 <option value="">Make a selection</option>
                 <option value="1">1</option>
                 <option value="2">2</option>
                 <option value="3">3</option></select>
             </td>
         </tr>    
    </table>    
    <table class="tab tab_Physical" border="0" style="display:none">
        <tr><th><label for="id_mas_tras_cof">Mass Transfer Coefficient (m/s):</label></th>
            <td><input type="text" name="mas_tras_cof" id="id_mas_tras_cof" /></td></tr>
    </table>
    <table align="center">
        <tr><td><input class="back" type="button" value="Back" /></td>
            <td><input class="next" type="button" value="Next" /></td>
            <td><input class="submit" type="submit" value="Submit" /></td></tr>
    </table></form>

JS

$(document).ready(function () {
    var tab_pool = ["tab_Chemical", "tab_Application", "tab_Physical"];
    var visible = $(".tab:visible").attr('class').split(" ")[1];
    var curr_ind = $.inArray(visible, tab_pool);
    $(".submit").hide();
    $(".back").hide();

    var validator = $('form').validate({
        ignore: 'input[type="button"],input[type="submit"]',
        rules: {
            wat_hl: {
                required: true,
                number: true
            },
            noa: {
                required: true
            },
            mm1: {
                required: true
            },            
            mm2: {
                required: true
            },              
            mm3: {
                required: true
            },              
            dd1: {
                required: true
            },            
            dd2: {
                required: true
            },              
            dd3: {
                required: true
            },               
            mas_tras_cof: {
                required: true
            }
        }
    });


    $('.next').click(function () {
        var tab = $(".tab:visible");

        var valid = true;
        $('input', tab).each(function (i, v) {
            valid = validator.element(v) && valid;
        });

        if (!valid) {
            return;
        }

        if (curr_ind < 2) {
            $(".tab:visible").hide();
            curr_ind = curr_ind + 1;
            $("." + tab_pool[curr_ind]).show();
            $(".submit").hide();
            $(".back").show();
        }
        if (curr_ind == 2) {
            $(".submit").show();
            $(".next").hide();
        }
    });

    $('.back').click(function () {
        if (curr_ind > 0) {
            $(".tab:visible").hide();
            curr_ind = curr_ind - 1;
            $("." + tab_pool[curr_ind]).show();
            $(".submit").hide();
            $(".next").show();
        }
        if (curr_ind == 0) {
            $(".back").hide();
        }
    });

    var i = 1
    $('.tab_Application').append('<tr id="noa_header" style="display:none"><th width="18%">App#</th><th width="18%">Month</th><th width="18%">Day</th>');

    $('#id_noa').change(function () {
        var total = $(this).val()
        $('tr[id*="noa_header"]').show()

        while (i <= total) {
            $('.tab_Application').append('<tr class="tab_noa1"><td><input type="text" size="5" value="' + i + '"  disabled/></td><td><input type="text" size="5" name="mm' + i + '" id="id_mm' + i + '""/></td><td><input type="text" size="5" name="dd' + i + '" id="id_dd' + i + '""/></td>');
            i = i + 1;
        }
        while (i - 1 > total) {
            $(".tab_Application tr:last").remove();
            i = i - 1
        }
        $('</table>').appendTo('.tab_Application');
    })
});  

Question 1: Your code contains:

$('input', tab).each(function (i, v) {
        valid = validator.element(v) && valid;
    });`

But a select is not an input . Change it to $('input,select', tab) .

Question 2: See this question for how to apply rules by class. Also, if an input has a class that's the name of a validation method, the method will be applied, so you can use class="required number" on an input. And the plugin also recognizes HTML5 validation tags, so you can say <input type="number" required/> and it will perform the appropriate validations.

Question 3: You can use a regexp. See this question .

Quote OP:

2) Is there an express way in jQuery validation plugin that I can validate fields with similar names? For example, I have fields with name mm1 , mm2 , and mm3 . Can use some shortcut way such as mm* in the validation rule?

Yes.

$('[name*="mm"]').each(function() {
    $(this).rules('add', {
        required: true,
        digits: true,
        messages: {
            required: "custom message required",
            digits: "custom message digits"
        }
    });
});

See Docs : http://docs.jquery.com/Plugins/Validation/rules#.22add.22rules

DEMO : http://jsfiddle.net/g7XES/


Responding to OP's comments:

Inside the additional-methods.js file is a rule called integer .

jQuery.validator.addMethod("integer", function(value, element) {
    return this.optional(element) || /^-?\d+$/.test(value);
}, "A positive or negative non-decimal number please");

DEMO : http://jsfiddle.net/v82sZ/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM