Jquery validation

0

I'm trying to solve a validation problem using Jquery, but I can not use the jquery lib itself to do this validation.

So far I have managed to do a validation using only jquery, however I came across the following problem, when user does not fill in the time field, msg appears "Required Field" on the side of the time field, but when I go give submit again with the field not filled, it repeats the msg again.

Jquerycode

$(document).ready(function(){$('#region_btnSave').click(function(){vartxtHValue=$("#region_tabVersao_N_txtH").val();
        var emailAddressValue = $("#region_tabVersao_N_txtM").val();
        if (emailAddressValue == '' || txtHValue == '') {
            $("#region_tabVersao_N_txtH").after('<span class="error">Campo Obrigadorio</span>',null);
            return false;
        }
        else {
            //not all text fields are valid
            $("#region_tabVersao_N_txtH").after('', null);
        }
    });
});

Validation field:

<td> 
    <asp:TextBox ID="txtH" runat="server" MaxLength="3" Width="40" />
    <span class="error"></span>
</td>
    
asked by anonymous 22.01.2018 / 14:52

1 answer

1

The problem of repetition is related to this line:

$("#region_tabVersao_N_txtH").after('<span class="error">Campo Obrigadorio</span>',null);

Where .after adds element after $("#region_tabVersao_N_txtH") .

I will use an example of what can be done, but may not be the best option in your case, since the html code is not complete.

In this part modify:

<span class="error" id="errortxtH"></span>

And here modify these lines:

$(document).ready(function()
{
    $('#region_btnSave').click(function ()
    {  
        var txtHValue = $("#region_tabVersao_N_txtH").val();
        var emailAddressValue = $("#region_tabVersao_N_txtM").val();
        if (emailAddressValue == '' || txtHValue == '') {
            $("#errortxtH").text("Campo Obrigatorio");
            return false;
        }
        else {
            //not all text fields are valid
            $("#errortxtH").text("");
        }
    });
});

The jquery .text function was used to put the content (Required field) in <span> and remembering that this code only adds required field to the course time input.

See if that solves your question.

    
22.01.2018 / 15:33