Search code examples
javascriptfunctionreturn-valuereturnform-submit

Check if multiple functions are true, then do something


I am stuck on what I thought was a simple PEBCAK error on my part. I am trying to verify all of my functions are true before I submit a form, but cannot figure for the life of me what is wrong. Below is my javascript code:

function checknewaccount(){
if(emailvalid()&& checkname() && passwordcheck())
{
    return true;
}
else{
    return false;
}
}


function emailvalid()
{
      if(email condition)
      {
          return true;
      }
      else {
          return false;
      }
}

function checkname())
{
      if(name condition)
      {
          return true;
      }
      else {
          return false;
      }
}

function passwordcheck(){
      if(password condition)
      {
          return true;
      }
      else {
          return false;
      }
}

html below:

<form  id="newaccount" name="newaccount" method="post"  onSubmit="accountcode.php">
<input type="text" id="email" onBlur="emailvalid()"/>
<input type="text" id="username" onBlur="checkname()" />
<input type="password" id="password"  onkeyup="passwordcheck()"/>
<input type="submit" value="New" onClick="return checknewaccount()"/>
</form>

When i click "New, nothing happens, and I know the accountcode.php is not running, because nothing happens on the database end and there are no errors reported.

To sum up, my question is how checknewaccount() does not work? Does it have something to do with how I am calling them?

I am new to javascript so if I am completely off on my implementation, I apologize. Thank you very much for the help!


Solution

  • you've got the form syntax wrong - onsubmit = the name of the js function to call, action = the url...

    <form action="accountcode.php" id="newaccount" name="newaccount" method="post"  onSubmit="return checknewaccount()">
    <input type="text" id="email" onBlur="emailvalid()"/>
    <input type="text" id="username" onBlur="checkname()" />
    <input type="password" id="password"  onkeyup="passwordcheck()"/>
    <input type="submit" value="New"/>
    </form>
    

    Fully tested code:

    <html>
        <head>
            <script type="text/javascript">
            function checknewaccount() {
                return emailvalid() && checkname() && passwordcheck();
            }
    
            function emailvalid() {
                 var emailAddress = document.getElementById('email').value;
                 return (emailAddress=='test');
            }
    
            function checkname() {
                 return true;
            }
    
            function passwordcheck() {
                 return true;
            }
    
            </script>
        </head>
        <body>
        <form action="#" onsubmit="return checknewaccount();">
            <input type="text" id="email" name="email"/>
            <input type="submit"/>
        </form>
        </body>
    </html>
    

    The form in the above code will only submit if the textbox has a value of test

    A slightly better implementation would be:

    <html>
        <head>
            <script type="text/javascript">
            function checknewaccount() {
                if(emailvalid() && checkname() && passwordcheck()) {
                    return true;
                } else {
                    document.getElementById('validation').innerHTML = 'Validation failed!';
                    return false;
                }
            }
    
            function emailvalid() {
                 var emailAddress = document.getElementById('email').value;
                 return (emailAddress=='test');
            }
    
            function checkname() {
                 return true;
            }
    
            function passwordcheck() {
                 return true;
            }
    
            </script>
        </head>
        <body>
        <div id="validation"></div>
        <form action="#" onsubmit="return checknewaccount();">
            <input type="text" id="email" name="email"/>
            <input type="submit"/>
        </form>
        </body>
    </html>
    

    As this at least tells the user the form wasn't submitted. Even better would be to give the user a more detailed reason why but that's beyond the scope of this question...