Jquery validation

Asked

Viewed 45 times

0

I am trying to solve a validation problem using Jquery ,but I cannot use jquery’s own lib to do this validation.

So far I managed to do a validation using only jquery ,but I came across the following problem ,when user does not fill the time field,"Required Field" appears on the side of the time field ,but when I will give Ubmit again with the unfilled field ,he repeats the msg again.

inserir a descrição da imagem aqui

Code of the Jquery

$(document).ready(function()
{
    $('#region_btnSave').click(function ()
    {  
        var txtHValue = $("#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>
  • If possible put the full html code.

  • Sometimes this is related to duplicate files, or a jquery or your own javascript file, tries to check the code on another standalone page and clear of scripts, if the error does not occur, is some duplicate file.

1 answer

1


The repetition problem is related to this line:

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

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

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

In this part modify:

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

And in this one, 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 function was used .text jquery, to place the content (Required field) in the <span> and remembering that this code only adds mandatory field for course time input.

See if that clears your doubt.

  • It worked, thank you very much!

  • would know if you have how to make that same code with ajax so as soon as it puts thing in the field ,the span goes away

  • With ajax would have to take a look, but can be done via event onkeydown for example, when the first key is typed in the input, performs the X event that deletes the contents of the <span>.

  • @Robsonoliveira If you are using Asp you can use the . change function that should work. Anything open another question.

  • To be able to do with keydown ,I’m using yes However on this screen just qeu the validation of Asp does not work, why I’m having to do with jquery(is already a system that existed )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.