victorjonsson / jQuery-Form-Validator

[DISCONTINUED] jQuery plugin that makes it easy to validate user input while keeping your HTML markup clean from javascript code.
973 stars 477 forks source link

Multiple error messages #706

Open kcrob2 opened 6 years ago

kcrob2 commented 6 years ago

Good day,

I've got a input field with multiple checks on validation. I've created a new validator for this field. But when for example someone puts in a wrong email and submits, it gets the standard message. When the user changes the email in the input, the message of my created validator shows instantly.

Here's my code

<form id="user_form" action="/aanmelden-save" method="post">
<input type="text" class="form-control" id="email" name="email" placeholder="E-mailadres" data-validation="required unique_email email length" autofocus maxlength="49" data-validation-length="max49">
<input type="button" class="next btn btn-success pull-right signup_2" value="Volgende" />
</form>

<script>
$.formUtils.addValidator({
            name : 'unique_email',
            validatorFunction : function(value, $el, config, language, $form) {
                if ($("#email").is(":focus")) {
                    return false;
                }
                var data = null;

                $.ajax({
                    async: false,
                    dataType: "json",
                    type: 'GET',
                    url: '/aanmelden-check-email',
                    data: {
                        email: value,
                    },
                    success: function(result){
                        data = true;
                        console.log(data);
                        if (result == 1) {
                            data = false;
                        }
                    },
                    error: function(req, err){ console.log('my message' + err); }
                });

                return data;
            },
            errorMessage : 'Het door u opgegeven e-mailadres bestaat reeds in onze database. Gebruik een nieuw e-mailadres of <a href="/wachtwoord/vergeten">vraag uw wachtwoord op</a>.',
            errorMessageKey: 'bad_unique_email'
        });

$.validate({
            form : '#user_form',
            lang : 'nl',
            decimalSeparator : ',',
            scrollToTopOnError : false,
            validateOnBlur : false
     });

Does someone knows a solution? Thanks in advance

kcrob2 commented 6 years ago
if ($("#email").is(":focus")) {
     return false;
 }

Removed that part and works like a charm