Search code examples
javascriptjqueryvalidationkeyuponkeyup

JQuery mailcheck on keyboard timer


I am using this mailcheck code found here: https://github.com/Kicksend/mailcheck That will give you a "suggestion" if you misspell your email incorrectly.

I have one input email box and a div where the suggestion text will appear:

<form id="mailcheck-test">
    <label for="email">Email</label>
    <input id="email" type="text">
</form>
<div id="hint"></div>

How do I apply a keyup jQuery timer to the input box, then have the mailcheck activate? Thanks!

UPDATE

Here is my updated code: var $email = $('#email'); var $hint = $("#hint"); var typingTimer; //timer identifier var doneTypingInterval = 1000; //time in ms, 5 second for example

$('#email').keyup(function(){
    $hint.css('display', 'none').empty();
clearTimeout(typingTimer);
$(this).mailcheck({
    suggested: function(element, suggestion) {
    typingTimer = setTimeout(function(){    
                if(!$hint.html()) {
        // First error - fill in/show entire hint element
        var suggestion = "Yikes! Did you mean <span class='suggestion'>" + "<span class='address'>" + suggestion.address + "</span>" + "@<a href='#' class='domain'>" + suggestion.domain + "</a></span>?";
            $hint.html(suggestion).fadeIn(150);
            } else {
            // Subsequent errors
        $(".address").html(suggestion.address);
        $(".domain").html(suggestion.domain);
            }
            }, doneTypingInterval);
        }
    });
});

$hint.on('click', '.domain', function() {
    // On click, fill in the field with the suggestion and remove the hint
    $email.val($(".suggestion").text());
    $hint.fadeOut(200, function() {
        $(this).empty();
    });
    return false;
});

Solution

  • I finally got it working! here is a working demo: http://jsfiddle.net/dswizzles/jCWFe/1

    var $email = $('#email');
    var $hint = $("#hint");
    var typingTimer;                //timer identifier
    var doneTypingInterval = 1000;  //time in ms, 5 second for example
    
    $('#email').keyup(function(){
        $hint.css('display', 'none').empty();
    clearTimeout(typingTimer);
    $(this).mailcheck({
        suggested: function(element, suggestion) {
            if(!$hint.html()) {
            // First error - fill in/show entire hint element
            var suggestion = "Yikes! Did you mean <span class='suggestion'>" + "<span class='address'>" + suggestion.address + "</span>" + "@<a href='#' class='domain'>" + suggestion.domain + "</a></span>?";
            typingTimer = setTimeout(function(){                  
                $hint.html(suggestion).fadeIn(150);
            }, doneTypingInterval);
            } else {
            // Subsequent errors
            $(".address").html(suggestion.address);
            $(".domain").html(suggestion.domain);
            }
            }
        });
    });
    
    $hint.on('click', '.domain', function() {
        // On click, fill in the field with the suggestion and remove the hint
        $email.val($(".suggestion").text());
        $hint.fadeOut(200, function() {
            $(this).empty();
        });
        return false;
    });