September 22, 2012

Jquery - Validate Multiple input controls Inside a Parent

function fnValidateChildFields(parentElement, selector) {
    var isValid = true;
 
    //Get Required Elements inside "Parent Element" By using selector
    //It will return filtered elements.
    var elements = jQuery(parentElement).find(selector);
 
    //For Each all fields and do your stuff
    jQuery.each(elements, function () {
 
        // Do validation Here..
 
        /* SAMPLE VALIDATION STARTS HERE */
 
        alert(this.id);
        alert(this.value);
        alert(this.getAttribute('class'));
 
        // Required Field validation
        if (this.value.length == 0)
            isValid = false;
 
        /* SAMPLE VALIDATION ENDS HERE */
 
    });
 
    return isValid;
}
 
 
function fnValidatePageII() {
    var isValid = false;
    try {
 
        // Parent ID name
        var parentElement = "#divParentID"
 
        // Class Name, It is used to get classes that needs to be replaced.
        var selector = ".className"
 
        if (fnValidateChildFields(parentElement, selector) == true)
            isValid = true;
    }
    catch (e) {
        alert(e);
    }
    return isValid;
}

No comments:

Post a Comment