2

I am currently using JQuery Validate plugin in along with some tip plugin to validate a form. I need to validate the e-mail address as well, other then just checking all fields have been filled out. I am familiar with the Validate rules function, however it causing a problem.

As soon as the e-mail evaluates to be invalid, with my current implementation, all following text fields appear to be invalid (by generating a plugin tip) and causes all following fields to have an annoying tip saying this field is required before the user even had a chance to fill them out.

I was wondering if there was a way to check if my e-mail validation function returned true from within the validate call, where if it does not return true, then consider the form invalid.

Later on I will need to implement a password regex function that will follow the same logic.

My Form Validation

 $(".form").validate({
            errorPlacement: function(error, element) {
            },
            highlight: function(element, errorClass, validClass) {
                $(element).css('border', '1px solid red');
                $('.addlefttip').qtip({
                    position: {
                        my: 'middle left', 
                        at: 'middle right'
                    },
                    show: {
                        event: 'focus'
                    },
                    hide:{
                       event: 'blur'
                    },
                    style: {
                            classes: 'qtip-blue qtip-shadow'
                        },
                        content: {
                            attr: 'alt'
                        },
                    });
                    $('.addtoptip').qtip({
                        position: {
                            my: 'bottom left', 
                            at: 'top right'
                        },
                        show: {
                            event: 'focus'
                        },
                        hide:{
                            event: 'blur'
                        },
                        style: {
                            classes: 'qtip-blue qtip-shadow'
                        },
                        content: {
                            attr: 'alt'
                        },
                    });
                },
                rules: {
                    email: {
                        minlength: 5
                    }
                },
                unhighlight: function(element, errorClass, validClass) {
                    $(element).css('border', '1px solid #ddd');
                }
            });

My e-mail validation:

    $('.email-field').change(function(){
            var email = $(this).val();

            var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

            if(re.test(email)) {
                document.getElementById('result').innerHTML = '<span style="color:green; font-style:italic; font-weight:normal;"><img src="http://myhousesforsaleindetroit.com/wp-content/uploads/2012/08/green-check-mark.png"> valid';
            } else {
                document.getElementById('result').innerHTML = '<img src="http://upload.wikimedia.org/wikipedia/en/archive/8/89/20070603232424!RedX.png" style="width:15px; margin:-2px 0px 0px 0px;"/> invalid';
            }
        });

I appreciate any suggestions!

Many thanks in advance!

2 Answers 2

1

I guess I'm not fully understanding your implementation. It seems pointless to use a form validation plugin and then bypass part of it with a custom change handler. Since you're using the jQuery Validate plugin, why not just use its built-in email rule?

Otherwise, if you like your email function better, turn it into a custom rule using the built-in addMethod() method.

$.validator.addMethod("myCustomRule", function(value, element) { 
      return /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(value); 
}, "Custom message for this rule");

It's implemented just like any other rule...

rules: {
   myfield: {
       myCustomRule: true
   }
}

As far as using qTip2 with jQuery Validate, I've done it like this...

$(document).ready(function () {

    $('#myform').validate({ 
        // any other options & rules,
        errorPlacement: function (error, element) {
            $(element).filter(':not(.valid)').qtip({
                overwrite: false,
                content: error,
                show: {
                    event: false,
                    ready: true
                },
                hide: false
            }).qtip('option', 'content.text', error);
        },
        success: function (error) {
            setTimeout(function () {
                $('form').find('.valid').qtip('destroy');
            }, 1);
        }
    });

});

DEMO, jQuery Validate with qTip2: http://jsfiddle.net/96AM4/


However lately, I've been using Tooltipster instead because its integration with jQuery Validate is somewhat cleaner.

$(document).ready(function () {

    // initialize Tooltipster on form input elements
    $('#myform input[type="text"]').tooltipster({ 
        trigger: 'custom', // default is 'hover' which is no good here
        onlyOne: false,    // allow multiple tips to be open at a time
        position: 'right'  // display the tips to the right of the element
    });

    // initialize jQuery Validate
    $('#myform').validate({
        // any other options & rules,
        errorPlacement: function (error, element) {
            $(element).tooltipster('update', $(error).text());
            $(element).tooltipster('show');
        },
        success: function (label, element) {
            $(element).tooltipster('hide');
        }
    });

});

Side-note:

It's best not to leave a callbcak function empty like this:

errorPlacement: function(error, element) {
},

Use this instead if your intention is to suppress the default error messages:

errorPlacement: function(error, element) {
    return false;
},

DEMO, jQuery Validate with Tooltipster: http://jsfiddle.net/kyK4G/

Sign up to request clarification or add additional context in comments.

13 Comments

+1, Thanks for the reply @Sparky. I am not using the email rule because anytime I use that rule and the rule evaluates to invalid, all text inputs after the invalid email evaluate to invalid after it, even BEFORE the user got a chance to fill it out. Tooltipster is interesting.... looking into it ;)
@MiGusta, the behavior you describe is not the normal default behavior of the plugin. Can you set up a jsFiddle?
Your right, It isn't initially, but it is in my case because I placed the qtip event listener in the highlight call for the validation plugin... I'll try to post the jsFiddle later tonight or tomorrow. Thanks.
I set up a jsFiddle to demonstrate the problem I am having: jsfiddle.net/V7v6R , I appreciate any suggestions. Note when you type in an invalid e-mail is entered then all input fields that follow become 'invalid' before the user even has a chance to fill them out.
Also, I wasn't able to addMethod() successfully, can you possibly edit your code so the new method returns true if email is valid and false otherwise, and I can somehow use it in the validate call?
|
0

The problem was caused due to me selecting $('.addlefttip').qtip({... within the highlight definition. Changing the selector to element fixed the problem described.

 highlight: function(element, errorClass, validClass) {
                $(element).css('border', '1px solid red');
                $(element).qtip({
                    position: {
                        my: 'middle left', 
                        at: 'middle right'
                    },
                    show: {
                        event: 'focus'
                   ...
                   ...
                   ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.