0

Friends! I Have this JavaScript and the Validade Form not Work After I Select a Ajax Populate SelectBox. If not Select and Populate with Ajax, Validade Form Work. When Try Select (District) and Ajax Populate SelectBox SubDistrict, Validade Form Not Work...

$.ajaxSetup({ cache: false });
$("#PriceForSale").hide();
$("#PriceForSeason").hide();
$("#PriceForRent").hide();

$(document).ready(function()
{
    $(document).on("change",'#frmPropertyAdd select[name^="frmCities"]', function() {
    $("select[name=frmDistricts]").html('<option value="0">Carregando...</option>');
    $.post("ajaxDistricts.php",
        {frmCities:$(this).val()},
        function(valor)
        {
            $("select[name=frmDistricts]").html(unescape(valor));
        })
    })
    $("select[name=frmDistricts]").change(function()
    {
        $("select[name=frmSubDistricts]").html('<option value="0">Carregando...</option>');
        $.post("ajaxSubDistricts.php",
        {frmDistricts:$(this).val()},
        function(valor)
        {
            $("select[name=frmSubDistricts]").html(unescape(valor));
        })
    })

    function validateForm()
    {
        var refid = $("#frmRefId");
        var city = $("#frmCities").val();
        var district = $("#frmDistricts").val();
        var subdistrict = $("#frmSubDistricts").val();
        var pType = $("#frmPropertiesTypes").val();
        var pSubType = $("#frmPropertiesSubTypes").val();

        alert("teste");
        alert(district);
        if (city == '' || city == NULL || city == '0' || city == 0)
        {
            alert("teste2");
            $('#msgResults').show();
            $("#msgResults").html('<div class="alert alert-block alert-danger fade in"><button data-dismiss="alert" class="close close-sm" type="button"><i class="fa fa-times"></i></button><h4><i class="icon-ok-sign"></i>ERRO AO TENTAR ENVIAR O FORMULARIO!</h4><p>É Necessario Selecionar a Cidade do Imóvel...</p></div>');
            return false;
        }
        else
        {
            alert("teste3");
        }
        if (district == '' || district == NULL || district == '0' || district == 0 || district == null)
        {
            alert("teste4");
            $('#msgResults').show();
            $("#msgResults").html('<div class="alert alert-block alert-danger fade in"><button data-dismiss="alert" class="close close-sm" type="button"><i class="fa fa-times"></i></button><h4><i class="icon-ok-sign"></i>ERRO AO TENTAR ENVIAR O FORMULARIO!</h4><p>É Necessario Selecionar o Bairro do Imóvel...</p></div>');
            return false;
        }
        if (subdistrict == '' || subdistrict == NULL || subdistrict == '0')
        {
            $('#msgResults').show();
            $("#msgResults").html('<div class="alert alert-block alert-danger fade in"><button data-dismiss="alert" class="close close-sm" type="button"><i class="fa fa-times"></i></button><h4><i class="icon-ok-sign"></i>ERRO AO TENTAR ENVIAR O FORMULARIO!</h4><p>É Necessario Selecionar o Sub-Bairro do Imóvel...</p></div>');
            return false;
        }
        if (pType == '' || pType == NULL || pType == '0')
        {
            $('#msgResults').show();
            $("#msgResults").html('<div class="alert alert-block alert-danger fade in"><button data-dismiss="alert" class="close close-sm" type="button"><i class="fa fa-times"></i></button><h4><i class="icon-ok-sign"></i>ERRO AO TENTAR ENVIAR O FORMULARIO!</h4><p>É Necessario Selecionar o Tipo do Imóvel...</p></div>');
            return false;
        }
        if (pSubType == '' || pSubType == NULL || pSubType == '0')
        {
            $('#msgResults').show();
            $("#msgResults").html('<div class="alert alert-block alert-danger fade in"><button data-dismiss="alert" class="close close-sm" type="button"><i class="fa fa-times"></i></button><h4><i class="icon-ok-sign"></i>ERRO AO TENTAR ENVIAR O FORMULARIO!</h4><p>É Necessario Selecionar o Sub-Tipo do Imóvel...</p></div>');
            return false;
        }
        if (refid.val().length < 1)
        {
            $('#msgResults').show();
            $("#msgResults").html('<div class="alert alert-block alert-danger fade in"><button data-dismiss="alert" class="close close-sm" type="button"><i class="fa fa-times"></i></button><h4><i class="icon-ok-sign"></i>ERRO AO TENTAR ENVIAR O FORMULARIO!</h4><p>É Necessario Digitar a ID de Referencia do Imóvel...</p></div>');
            return false;
        }
    }


    $(document).on("submit",'#frmPropertyAdd', function(event)
    {
        $('#msgResults').hide();
        if (!validateForm())
        {
            return false;
        }
        else
        {
            return true;
        }
    });
});

what i do? i belive this correct, but not work...

Edited by peter_budo: JavaScript is not Java!!! Moving to JavaScript section

2
Contributors
1
Reply
11
Views
2 Years
Discussion Span
Last Post by AleMonteiro
0

What do you mean by not work? What error do you get? what alerts do you get before the error?
Are you sure the ajax loaded options have a value property?

Another thing.. your validation is not good. If you're so uncertain of what you're getting, try something like this:

function _hasValue(val)
{
    return val !== undefined && val !== null && val != 0 && val != '';
}

if ( ! _hasValue(district) ) {
    // show error
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.