简体   繁体   中英

Validation for fields into form no for all fields

I want after click on button(Click Me), validation check just for field 2-1 and field 2-2 no for all files that has class .required (... .closest('form')...), how is it in my code?

DEMO: (in here when that you click on button it work for all field that have class .required but i want just check field into form closest('form') ): http://jsfiddle.net/ZsPyy/2/

function required_valid() {
    var result = true;
    $('.required').each(function () {
        if (!$(this).val()) {
            //var cssObj=;
            $(this).css("background", "#ffc4c4");
            result = false;
        }
        $(this).keyup(function () {
            $(this).css("background", "#FFFFEC");
        })
    });
    return result;
}
$('button').live('click', function (e) {
    e.preventDefault();
    var passed = true;
    //passed = required_selectbox() && passed;
    passed = required_valid() && passed;
    if (!passed) {
        return false;
    }
});

Try this code: http://jsfiddle.net/ZsPyy/4/

I have passed the button to the required_valid function. So we can get the btn's parent form.

function required_valid(btn) {
    var result = true;
    $(btn).closest("form").find('.required').each(function () {
        if (!$(this).val()) {
            //var cssObj=;
            $(this).css("background", "#ffc4c4");
            result = false;
        }
        $(this).keyup(function () {
            $(this).css("background", "#FFFFEC");
        })
    });
    return result;
}
$('button').live('click', function(e) {
    e.preventDefault();
    var passed = true;
    //passed = required_selectbox() && passed;
    passed = required_valid(this) && passed;
    if (!passed) {
        return false;
    }
});
function required_valid(sbtn) {
    var result = true;
    $(sbtn).closest("form").children('.required').each(function () {
        if (!$(this).val()) {
            //var cssObj=;
            $(this).css("background", "#ffc4c4");
            result = false;
        }
        $(this).keyup(function () {
            $(this).css("background", "#FFFFEC");
        })
    });
    return result;
}
$('button').live('click', function(e) {
    e.preventDefault();
    var passed = true;
    //passed = required_selectbox() && passed;
    passed = required_valid(this) && passed;
    if (!passed) {
        return false;
    }
});

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