Hesham Saeed
Hesham Saeed

Reputation: 5378

Javascript validate form is not preventing submit

I have a form and I do JavaScript validation function onsubmit:

<form method="post" onsubmit="return validateForm(this.id)" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data" id='addjournalinfoform' style='border:0px;width:90%' class="formular" >
 
    <fieldset>
        <legend>
        Journal
        </legend>
        <label>
            <span>* ISSN:</span><br />
            <input type='text' onfocusout="validateField('issn')" class='text-input' value="" name="issn" id="issn" style='width:30%'>
        </label>
   </fieldset>

</form>

and this is the function:

function validateForm(form)
{
    if(form=="addjournalinfoform")
    {
        alert('onSubmit');
        if($('#issn').val()=="")
        {
            $('#issnMsg').hide(500);
            $('#issnMsg .formErrorContent').html("* This field is required!<br />");
            $('#issnMsg').css('margin-left', $('#'+field).width()-20);
            $('#issnMsg').show(500);
            $('#issn').focus();
            $('html:not(:animated),body:not(:animated)').animate({scrollTop: $('#issn').offset().top-50}, 0);
            return false;
        }
        else
        {
            return true;
        }
        
    }
    return false;
}

The problem is even when the function returns false, the form is still submitted. What am doing wrong, and how to prevent the submit?

Upvotes: 0

Views: 1044

Answers (2)

kapa
kapa

Reputation: 78691

In this line:

$('#issnMsg').css('margin-left', $('#'+field).width()-20);

The variable field is not defined.

Uncaught ReferenceError: field is not defined

You must learn how to debug your own applications, because you won't get far without it. Read about using the Console in whatever browser you are using.

You can find information about opening the console in your browser in this Webmasters.SE question.

Upvotes: 4

rogal111
rogal111

Reputation: 5933

open console (firebug, chrome, opera, ie9 have that). Check if there is no errors while executing validateForm().

I guess is there error in line:

 $('#issnMsg').css('margin-left', $('#'+field).width()-20);

There is an error:

 Uncaught ReferenceError: field is not defined

Do you have declared field variable?

Upvotes: 3

Related Questions