-2

I made a simple form with jquery and javascript, but the email verification (makes sure it has @ or . in it, does not seem to work.

Here is the JSFiddle: http://jsfiddle.net/LCBradley3k/xqcJS/11/

Here is the code for the validation. Is it in the wrong spot?

function validateForm() {
  var x = document.forms["signup"]["email"].value;
  var atpos = x.indexOf("@");
  var dotpos = x.lastIndexOf(".");
  if(atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
    $('#answer').html('Not a valid email')
    return false;
  }
}
hexacyanide
  • 88,222
  • 31
  • 159
  • 162
James Mitchell
  • 2,387
  • 4
  • 29
  • 59

1 Answers1

3

First, your code contains an error because you're missing }. Second, yo don't call validateForm when button join is clicked.

$('#join').click(function () {

            var correct = true;
            var validEmail = true;

            $('input[type="text"]').each(function (indx) {
                var $currentField = $(this);
                if ($currentField.val() === '') {
                    $currentField.addClass('empty');
                    correct = false;
                    $currentField.one('keydown', function () {
                        $currentField.removeClass('empty');
                    });
                } else {
                    $currentField.removeClass('empty');
                }

            });              

            function validateForm() {
                var x = document.forms["signup"]["email"].value;
                var atpos = x.indexOf("@");
                var dotpos = x.lastIndexOf(".");
                if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {                       
                    correct = false;
                    validEmail = false;
                }
            }

            validateForm();

            if (correct) {
                $('#answer').html('Thank You!');
                setTimeout(function () {
                    $('.inputs').hide("slide", { direction: "up" }, 1000);
                }, 2000);
            } else {
                if(validEmail)
                     $('#answer').html('Please fill highlighted fields.');
                else
                     $('#answer').html('Not a valid email');
            }
        });
Iswanto San
  • 18,263
  • 13
  • 58
  • 79
  • Thanks so much for your help. Just one more quick question. as you can see in the code (where it says if(correct)), I have it slide up. For some reason, even when it says not a valid email, that slideUp() still goes into effect. How would I stop that from happening? – James Mitchell Mar 07 '13 at 00:14
  • 1
    @JamesMitchell: you need to store the `validateForm` result..see my edited answer – Iswanto San Mar 07 '13 at 00:19
  • The current code does not say "Not a valid email", it just says "Please fill highlighted fields" – James Mitchell Mar 07 '13 at 00:25
  • Yes, it will show `Not a valid email` first and then replaced by `Please fill highlighted fields.` – Iswanto San Mar 07 '13 at 01:58