0

Validations is working fine, if valid email is entered, displays a hidden box if not valid is showing a message error on a overlay, but how to check if keydown or return? any ideas guys?

This is my code, which can also be accessed in a jsFiddle:

function validateEmail() {
    var validEmail = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    $('#user_email').blur(function(){
        if (validEmail.test(this.value)) {
            $('.masked-check').fadeIn(300)
        }
        else {
            $('.masked-check').fadeOut(300);
            $('.overlay').append('<div class="alert alert-error" id="errors"></div>');
            showOverlay($('#errors').html('Please enter a valid email...'));

        }
    }); 
}

validateEmail();

I not sure if this is the way to do it but works for me now, the only issue is not listening to a keypress keyup or keydown. Here is the updated http://jsfiddle.net/creativestudio/bKT9W/3/ I am not sure is this is the best way to do it but works for now.

    function validateEmail() {
        var validEmail = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        $('#user_email').blur(function(){

        $('.masked-check').hide();

            if (validEmail.test(this.value)) {
                $('.masked-check').fadeIn(300)
            }
            else {
                $('.masked-check').fadeOut(300);
                $('.overlay').append('<div class="alert alert-error" id="errors"></div>');
                showOverlay($('#errors').html('Please enter a valid email...'));
            }

            if ($('#user_email').val()=='') {
                $('.masked-check').hide()
            }

        }); 
    }

    validateEmail();
user1547007
  • 267
  • 1
  • 10
  • 22
  • You probably won't want to do it on `keydown`... For example, if I'm typing an email address: `myemail@email.com`, I would get the overlay for every character I press until the email is valid - which is about 16 times in the above example. – ahren Nov 08 '12 at 20:55

3 Answers3

2

I believe what you're trying to achieve is for the email validation to run after each key-stroke and to keep notifying the user if their email was valid or not until it is valid.

There is a jQuery .keypress() event to identify a keypress event.

I got this working here: http://jsfiddle.net/bKT9W/2/

EDIT: I believe Peter's answer below is much better: https://stackoverflow.com/a/13298005/1415352

He suggests a better regex and also the .keyup() event which solves the backspace issue which arises when using .keypress()

Community
  • 1
  • 1
keithxm23
  • 1,280
  • 1
  • 21
  • 41
2

I like the idea of fade in/out based on valid/invalid input.

I played around a bit and following seem to be working for me ok: http://jsfiddle.net/yc9Pj/

function validateEmail(){

    var validEmail = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    $('#user_email').keyup(function(){
    if (validEmail.test(this.value)) {
        $('.masked-check').fadeIn(300)
    }
    else {
        $('.masked-check').fadeOut(300);
        //alert('Please enter a valid email');                                
    }
    });    
}

validateEmail();​

please note, that I adapted regex for email based on this reply: Using a regular expression to validate an email address

Moreover keyup worked best for me, as keypress didn't handle backspace (solution found here: jQuery: keyPress Backspace won't fire?)

Community
  • 1
  • 1
Peter Butkovic
  • 11,143
  • 10
  • 57
  • 81
0

Change your function "validateEmail" to the following:

function validateEmail(){

    var validEmail = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    $('#user_email').keydown(function(event){
        if (event.keyCode !== 13) {
            return;
        }
        if (validEmail.test(this.value)) {
            $('.masked-check').fadeIn(300)
        }
        else {
            //$('.masked-check').fadeOut(300);
            alert('Please enter a valid email');                                
        }
    });    
}