Javascript return false, alert not working

13,529

Solution 1

access every element as document.getElementById... and in form tag write this onsubmit="return check();" instead if onsubmit="check();"

Solution 2

You are missing return here:

<FORM ACTION="step2.php" METHOD="POST" onsubmit="return check();">
Share:
13,529

Related videos on Youtube

cream
Author by

cream

Everything I've ever needed to know I learned from Google. I've had an interest in code since I was 16. Very little formal education after high school though. However, I do know a few big words. Circumlocution. Impressed?

Updated on June 04, 2022

Comments

  • cream
    cream almost 2 years

    i'm trying to make sure certain fields are not left blank in my form. it seems simple enough but for some reason it's not working. The alert is not showing, and return false is not working (it continues to post blank entries into my database) please help, what am i doing wrong. thank you!

    the script:

    function check(){
    var name = getElementById('name');
    var date = getElementById('date');
    var pri = getElementById('pri');
    var asapc = getElementById('asapc');
    var asapn = getElementById('asapn');
    var obr = getElementById('obr');
    var obc = getElementById('obc');
    var obn = getElementById('obn');
      if (name.value == "" || date.value == "" || pri.value == "not" || asapc.value == "" ||  asapn.value == "" || obr.value == "" || obc.value == "" || obn.value == "") {
        alert( "One or more fields were not filled out." );
        return false ; }
        return true;
     }
    

    The code:

    <FORM ACTION="step2.php" METHOD="POST" onsubmit="check();">
    <!-- fields here -->
    <INPUT TYPE="submit" VALUE="CONTINUE">
    
  • j08691
    j08691 over 11 years
    Assuming a field was left blank, shouldn't the alert still fire?