Dica ASP.NET e Javascript: RadioButtonList

Salve desenvolvedores,

o Félix, brother, me perguntou se já havia feito algum script para validar se algum item de um RadioButtonList estava selecionado.

A função é simples, recupera os itens da coleção (getElementByName) através do ClientID e depois percorre a lista verificando se tem algum item marcado. Se tiver, o retorno é verdadeiro. Além da propriedade checked, poderiamos recuperar o valor selecionado, através do value.

Código JavaScript

function validarRBL()
{
    checkboxList = document.getElementsByName('<%=RadioButtonList1.ClientID %>');

    for (var i=0; i < checkboxList.length; i++)
        if ( checkboxList[i].checked )
            return true;

    return false;
}

abraços,

Leave a Reply