How to change validation text according to each type of validation in the Start Date field

Asked

Viewed 2,700 times

1

I have a code that validates with jquery validate and is 100% functional.

When validation occurs, jQuery Validate always displays what is written in the field title of input. Since I have different validations for some fields, how do I show the correct validation message? The way it’s always presented is the message that’s in the field title.

HTML

        <table>
            <tr>
                <td><label for="numeroContrato">N. Contrato:</label></td>
                <td><input type="text" id="numeroContrato"
                    name="numeroContrato" class="input-medium"
                    placeholder="Numero do Contrato" maxlength="10"
                    title="Preencha o campo N&uacute;mero do Contrato" />
                </td>
                <td></td>
            </tr>
            <tr>
                <td><label for="dataInicial">Data Inicial:</label></td>
                <td><input type="text" name="dataInicial" value=""
                    placeholder="Data Inicial" style="width: 150px"
                    title="Preencha o campo Data Inicial"> <script
                        type="text/javascript">
                        $(function() {
                            $('*[name=dataInicial]').appendDtpicker({
                                "closeOnSelected" : true,
                                "calendarMouseScroll" : false,
                                "minuteInterval" : 15,
                                "autodateOnStart" : false
                            });
                        });
                    </script></td>
                <td></td>
            </tr>

JAVASCRIPT

jQuery(document)
.ready(
    function() {

        //adicionavalidacaoCPF();
        adicionaValidacaoComparacaoDatas();
        adicionaValidacaoDateBR();

        var validator = $("#ajax_form_inclusao")
                .validate(
                        {
                            debug : true,
                            errorElement : "em",
                            errorContainer : $("#warning, #summary"),
                            errorPlacement : function(error,
                                    element) {
                                error.appendTo(element.parent("td")
                                        .next("td"));
                            },
                            success : function(label) {
                                label.text("ok!").addClass(
                                        "success");
                            },
                            rules : {
                                numeroContrato : {
                                    required : true
                                },
                                dataInicial : {
                                    required : true,
                                    dateBR : true
                                },
                                dataFinal : {
                                    required : true,
                                    dateBR : true
                                },
                                valorPF : {
                                    required : true,
                                    number: true
                                },
                                qtdPFContratado : {
                                    required : true,
                                    number: true
                                },
                                status : {
                                    required : true
                                }
                            },
                            submitHandler : function(form) {

EDIT 1:

Marcelo, your suggestion worked perfectly. However I’m having difficulty now positioning the message to the right of the field.

Look at the codes:

HTML

<form class="form-horizontal" method="post" action="" role="form"
        id="ajax_form_inclusao">
        <div id="alert_placeholder"></div>
        <input type="hidden" name="acao" value="incluirContrato" />
        <div class="control-group">
            <label for="numeroContrato">N. Contrato:</label> <input type="text"
                class="form-control" id="numeroContrato" name="numeroContrato"
                placeholder="Numero do Contrato" maxlength="10" /><br />
                <label for="error" class="error"></label>
        </div>
        <div class="control-group">
            <label for="dataInicial">Data Inicial:</label>
            <div class="input-group">
                <input type="text" id="dataInicial" name="dataInicial"
                    class="form-control" value="" placeholder="Data Inicial"
                    style="width: 150px" /><br />
                <script type="text/javascript">
                    $(function() {
                        $('*[name=dataInicial]').appendDtpicker({
                            "closeOnSelected" : true,
                            "calendarMouseScroll" : false,
                            "minuteInterval" : 15,
                            "autodateOnStart" : false
                        });
                    });
                </script>
                <label for="error" class="error"></label>
            </div>
        </div>
        <div class="control-group">
            <label for="dataFinal">Data Final:</label>

            <div class="input-group">
                <input type="text" id="dataFinal" name="dataFinal"
                    class="form-control" value="" placeholder="Data Final"
                    style="width: 150px" /><br />
                <script type="text/javascript">
                    $(function() {
                        $('*[name=dataInicial]').appendDtpicker({
                            "closeOnSelected" : true,
                            "calendarMouseScroll" : false,
                            "minuteInterval" : 15,
                            "autodateOnStart" : false
                        });
                    });
                </script>
                <label for="error" class="error"></label>
            </div>
        </div>
        <div class="control-group">
            <label for="valorPF">Valor do PF:</label> <input type="text"
                class="form-control" id="valorPF" name="valorPF"
                placeholder="Valor do PF" maxlength="10" /><br />
                <label for="error" class="error"></label>
        </div>
        <div class="control-group">
            <label for="qtdPFContratado">Qtde. de PF Contratado:</label> <input
                type="text" class="form-control" id="qtdPFContratado"
                name="qtdPFContratado" placeholder="Quantidade de PF"
                maxlength="20" /><br />
                <label for="error" class="error"></label>
        </div>
        <div class="control-group">
            <label for="status">Status:</label> <select name="status"
                class="status" id="status">
                <option value="">-- Selecione --</option>
                <option value="A">Ativo</option>
                <option value="I">Inativo</option>
            </select><br />
            <label for="error" class="error"></label>
        </div>
        <div class="modal-footer">
            <a href="#" class="limpar"><button type="reset" class="btn">Limpar</button>
            </a>
            <button type="submit" class="btn btn-primary" value="Salvar">Salvar</button>
        </div>
    </form>

JAVASCRIPT:

        var validator = $("#ajax_form_inclusao")
                .validate(
                        {
                            rules : {
                                numeroContrato : {
                                    required : true
                                },
                                dataInicial : {
                                    required : true,
                                    dateBR : true
                                },
                                dataFinal : {
                                    required : true,
                                    dateBR : true
                                },
                                valorPF : {
                                    required : true,
                                    number: true
                                },
                                qtdPFContratado : {
                                    required : true,
                                    number: true
                                },
                                status : {
                                    required : true
                                }
                            },
                            messages: {
                                numeroContrato: {
                                    required: "Campo N. Contrato &eacute; requerido",
                                    number: "Campo N. Contrato tem que ser numerico"
                                },
                                dataInicial: {
                                    required: "Campo Data Inicial &eacute; requerido",
                                    dateBR: "Campo Data Inicial inválido"
                                },
                                dataFinal: {
                                    required: "Campo Data Final &eacute; requerido",
                                    dateBR: "Campo Data Final inválido"
                                },
                                valorPF: {
                                    required: "Campo Valor PF &eacute; requerido",
                                    number: "Campo Valor PF tem que ser numerico"
                                },
                                qtdPFContratado: {
                                    required: "Campo qtdPFContratado &eacute; requerido",
                                    number: "Campo qtdPFContratado tem que ser numerico"
                                },
                                status: {
                                    required: "Campo Status &eacute; requerido"
                                }
                            },
                            highlight: function(element){
                                $(element).closest('.erro').removeClass('has-success').addClass('has-error');
                            },
                            success: function(element) {
                                $(element).closest('.erro').removeClass('has-error').addClass('has-success');
                                $(element).remove();
                            },
                            submitHandler : function(form) {
  • You’re missing code on the question, use the button edit to arrange that.

  • 1

    Hi Jorge, the code is functional. The submitHandler method code makes the persistence only. The code shown is already enough to help me.

1 answer

1

What I usually do is what is recommended on jQueryValidate’s website is the following:

$("form").validate({
    rules: {
        qtdPFContratado : {
            required : true,
            number: true
        }
    },
    messages: {
        qtdPFContratado: {
            required: "Campo qtdPFContratado &eacute; requerido",
            number: "Campo qtdPFContratado tem que ser numerico"
        }
    },
    highlight: function(element){
        $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
    },
    success: function(element) {
        $(element).closest('.form-group').removeClass('has-error').addClass('has-success');
        $(element).remove();
    }
});

Anything can find something else here

I didn’t put it all together, just so you’d have a real idea

  • What HTML would look like using this code above?

  • In this case, I’m using the bootstrap so I play the messages on .form-group , but not necessarily need to use. And the html of this input is the same basic. <input type="text" name="qtdPFContratado" id="qtdPFContratado" >

  • I will try here. Soon return with the result. Thanks for the support!

  • Marcelo, your susgestão worked perfectly. But now I’m having difficulties in the position of the validation messages in the form. See the EDIT 1 of the above question. If you can answer I am grateful.

  • Marcelo, I managed to solve it. Thanks for the support!

Browser other questions tagged

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