0

I am trying to validate email using javascript but for some reason it is not working.
PROBLEM:When I enter VALID email address it still alerts that that my address is faulty...
What I am missing here? Here's the code:

    $(document).ready(function(){

    $('.info').hide();
    $('#button').click(function(){

    var name = encodeURIComponent($('#name').val());
    var email = encodeURIComponent($('#email').val());
    var message = encodeURIComponent($('#message').val());
    var reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;



    //if name is empty
    if($('#name').val() == 0){

    $('.info').html('<p style="font-weight:bold;text-align:center;font-size:20px;"> Molimo unesite Vaše ime!</p>').fadeIn(1000);
    }
    //if name is less than three characters
    else if(name.length < 3){
    $('.info').html('<p style="font-weight:bold;text-align:center;font-size:20px;"> Molimo unesite ime duže od tri slova!</p>').fadeIn(1000);

    }
     // if email is empty
     if ($('#email').val() == 0){
    $('.info').html('<p style="font-weight:bold;text-align:center;font-size:20px;"> Molimo unesite Email!</p>').fadeIn(1000);

    }
    // if email AND name are empty
    if($('#email').val() == 0 && $('#name').val() == 0 ){
    $('.info').html('<p style="font-weight:bold;text-align:center;font-size:20px;"> Molimo unesite Email!<br/>Molimo unesite ime!</p>').fadeIn(1000);

    }
    //if mail adress is NOT valid
    if(reg.test(email)==false){
    //alert('please enter valid mail!');
    $('.info').html('<p style="font-weight:bold;text-align:center;font-size:20px;">Molimo unesite ispravan Mail!</p>').fadeIn(1000);
    }

            //if mail adress IS VALID (this is not working...)
    if($('#name')!= '' && reg.test(email) !=false ){

            //alert(data);
    var data = 'name=' + name + '&email=' + email + '&message=' + message;

           $.ajax({
           type:"POST",
           url:'kontakt2.php',
           cache:'false',//IE FIX
           data: data,

           success: function(){
           //alert('THX for your mail!');
           } //end sucess 
           });
           } //end if statment for VALID mail
           //return false prevent Redirection
           return false;
           });


           });
Dejo Dekic
  • 2,088
  • 4
  • 27
  • 50
  • 1
    So much easier to use a pre-packaged, well-tested solution: http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/ – bpeterson76 Jun 25 '12 at 16:55
  • 2
    Your regex is way too easy. Look at this post an the comments there: http://stackoverflow.com/questions/201323/how-to-use-a-regular-expression-to-validate-an-email-addresses – Michael Jun 25 '12 at 16:57
  • What is the valid e-mail address that it fails on? There are certainly plenty of valid e-mail addresses that that probably will fail on though. Email addresses are notoriously hard to validate fully so you might want to just do some basic bits like "is there an @, is there a . in the domain part" and do more robust checks if necessary server side (ie try to actually send mail to that address). – Chris Jun 25 '12 at 16:59

3 Answers3

2

the line

if($('#name')!= '' && reg.test(email) !=false )

should probably be

if($('#name').val()!= '' && reg.test(email) !=false )

(or just if (name != '' && reg.test(email) != false ) )

Rodolfo
  • 4,155
  • 23
  • 38
  • I'd also remove the explicit comparison to a boolean constant. It should be just `reg.test(email)` because it already returns a boolean. – Pointy Jun 25 '12 at 17:12
0

in this statement, you're checking to see if the input value is 0. that's not the way to check for an empty field:

if ( $('#name').val() == 0 )

a slightly better way would be:

if ( !$('#name').val() )
Omer Bokhari
  • 57,458
  • 12
  • 44
  • 58
0

I have managed to solve this by modifying this code:

if($('#name').val()!= '' && reg.test(email) !=false )

into this:

if(reg.test($('#email').val()) && $('#name').val()!='')

I am pretty sure that code above reg.test(email) part did not work because encodeURIComponent. Thx Rodolfo and Pointy for your answers:)

Dejo Dekic
  • 2,088
  • 4
  • 27
  • 50