0

If any of the form validations do not match, it is supposed to add one to a counter. At the end, if the counter is greater than zero, it is supposed to return false and not allow the form to submit.

I've written the code in Brackets. I've tried using both a hosted site and live preview to test the code, both of which result in the same issue. I've tried turning the function into a variable. I've tried different methods of taking the variables from the form. I've tried simply copying a different solution I found to this through google. Nothing seems to be working to get the validation to work as intended.

I apologize ahead of time for the wall of code.

JavaScript:

function checkAll(){
    var userNameVerification = "0-9a-zA-Z"; //must include upper and lowercase so that user may use caps
    var phoneNumberVerification = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/; //taken from w3resoucre for the setup of phone number verification
    var checker = 0;
    var userName = document.regForm.userName.value;
    var password = document.regForm.passwordvalue;
    var passwordVerify = document.regForm.passwordVerify.value;
    var firstName = document.regForm.firstName.value;
    var lastName = document.regForm.lastName.value;
    var email = document.regForm.email.value;
    var phoneNumber = document.regForm.phoneNumber.value;
    var signUpNewsletter = document.regForm.phoneNumber.value;


    //check if username is empty
    if(userName == ""){
        document.getElementById('errorUserName').innerHTML = "Username cannot be empty.";
        checker++;
    }

    //make sure username uses proper characters
    if(!userName.match(userNameVerification)){
        document.getElementById('errorUserName').innerHTLM = "Enter only numbers and letters.";
        checker++;
    }
    //check if password is empty or is shorter than 8 characters
    if(password == "" || password.lenth < 8) {
        document.getElementById('errorPassword').innerHTML = "Password should be at least 8 characters long.";
        checker++;
    }

    //make sure confirmation of password is not shorter than 8 and is not empty
    if(passwordVerify == "" || passwordVerify.lenth < 8) {
        document.getElementById('errorPasswordVerify').innerHTML = "Confirmation Password should be at least 8 characters long.";
        checker++;
    }
    //passwords match
    if(password != passwordVerify){
        document.getElementById('errorPasswordVerify').innerHTML = "Passwords do not match.";
        checker++;
    }

    //check if first name is empty
    if(firstName == ""){
        document.getElementById('errorFirstName').innerHTML = "First name cannot be empty.";
        checker++;
    }

    //check if last name is empty
    if(lastName == ""){
        document.getElementById('errorLastName').innerHTML = "Last Name cannot be empty.";
        checker++;    

    }

    //check if email is empty
    if(email == ""){
        document.getElementById('errorEmail').innerHTML = "Email cannot be empty.";
        checker++;
    }

    //check that @ and . are present
    if(email.indexOf("@",0) < 0 || email.indexOf(".",0) < 0){
        document.getElementById('errorEmail').innerHTML = "Enter a valid email address.";
        checker++;
    }

    //check if phone number is empty
    if(phoneNumber == ""){
        document.getElementById('errorPhoneNumber').innerHTML = "You must enter a phone number.";
        checker++;
    }

    //make sure phone number is in proper format
    if(!phoneNumber.match(phoneNumberVerification)){
        document.getElementById('errorPhoneNumber').innerHTML = "Enter a valid phone number in (XXX)XXX-XXXX format.";
        checker++;
    }

    //make sure one of the radio buttons are clicked
    if((signUpNewsletter[0].checked == false) && (signUpNewsletter[1].checked == false)){
        document.getElementById('errorSignUp').innerHTML = "Please select one of the options.";
        checker++;
    }

    //see if checker is greater than 0; if so, return false
    if(checker > 0){
        return false;
    }

} 

HTML:

<!DOCTYPE html>
<html lang="en-US">

<head>
    <title>Invitation Page</title>
    <link rel="stylesheet" type="text/css" href="css/main.css" />
    <script type="text/javascript" src="js/registration.js"></script>
</head>

<body>
    <header>
        <div class="top">
            <a class="logo" href="index.html">CapellaVolunteers<span class="dotcom">.org</span></a>
        </div>
        <nav>
            <ul class="topnav">
                <li><a href="index.html">Home</a>
                </li>
                <li><a href="invitation.html">Invitation</a>
                </li>
                <li><a href="volunteer.html">Volunteers</a>
                </li>
                <li><a href="gallery.html">Gallery</a>
                </li>
                <li><a href="registration.html" class="active">Registration</a>
                </li>
            </ul>

        </nav>
    </header>
    <section id="pageForm">
        <form name="regForm" action="confirmation.php" method="POST">

            <label for="userName">Username:</label>
            <input type="text" name="userName" placeholder="Enter your Username" />
            <span id="errorUserName"></span><br>

            <label for="Password">Password:
            </label>
            <input type="password" name="password" placeholder="Enter your Password" />
            <span id="errorPassword"></span><br>

            <label for="passwordVerify">Verify your Password:
            </label>
            <input type="password" name="passwordVerify" placeholder="Enter in your Password again" />
            <span id="errorPasswordVerify"></span><br>

            <label for="firstName">First Name:
            </label>
            <input type="text" name="firstName" placeholder="Enter your First Name" />
            <span id="errorFirstName"></span><br>

            <label for="lastName">Last Name:
            </label>
            <input type="text" name="lastName" placeholder="Enter your Last Name" />
            <span id="errorLastName"></span><br>

            <label for="email">Email:
            </label>
            <input type="text" name="email" placeholder="Enter your Email Address" />
            <span id="errorEmail"></span><br>

            <label for="phoneNumber">Phone Number
            </label>
            <input type="text" name="lastName" placeholder="Enter your Phone Number" />
            <span id="errorPhoneNumber"></span><br>

            <label for="signUpNewsletter">Sign up for newsletter:
            </label>
              <input type="radio" name="signUpNewsletter" value="Yes" checked> Yes 
                <input type="radio" name="signUpNewsletter" value="No"> No
                <span id="errorSignUp"></span><br>
            <input type="submit" value="Next step" onsubmit="return checkAll();">
        </form>
    </section>

    <footer>This events site is for IT3515 tasks.
    </footer>
</body>
</html>

I expect the form to not submit when the information is not validated (for example, I try submitting an empty form, which it should not allow me to do), but it actually submits the form no matter what information is inserted into the form.

Ian
  • 1
  • 1
    Possible duplicate of [How to prevent form from being submitted?](https://stackoverflow.com/questions/3350247/how-to-prevent-form-from-being-submitted) – Cray Jun 10 '19 at 04:41
  • Post your in a proper format so that people can run and help you or host it somewhere like codepen.io or jsfiddle. – Ramesh Navi Jun 10 '19 at 04:41
  • 1
    You should be preventing the submit, `return false` is not enough. Use `Event.preventDefault();`. – Cray Jun 10 '19 at 04:42
  • If you are using `preventDefault` in the form tag directly (in the `onsubmit`), be sure to use `event.preventDefault()`. Welcome to Stack Overflow! – thmsdnnr Jun 10 '19 at 04:43
  • 2
    You need to listen `onsubmit` on the _form_, not on the submit button. – Teemu Jun 10 '19 at 04:44
  • Thanks, not sure why my endless google searching didn't come up with it. Sorry about not formatting it properly, this is literally my first post here. Now I just need to figure out why it's not displaying any error messages and not letting me submit it at all :) – Ian Jun 10 '19 at 05:18

1 Answers1

0

Add your onsubmit call to form instead of button, it works. Don't know about your logic, but it works. Run this code!

function checkAll(){
  var condition = false;
  if(condition){
 alert ('All ok');
 return true;
  }
  alert('Something wrong');
  return false;
} 
   <section id="pageForm">
    <form name="regForm" action="confirmation.php" method="POST" onsubmit="return checkAll();">

        <label for="userName">Username:</label>
        <input type="text" name="userName" placeholder="Enter your Username" />
        <span id="errorUserName"></span><br>

        <label for="Password">Password:
        </label>
        <input type="password" name="password" placeholder="Enter your Password" />
        <span id="errorPassword"></span><br>

        <label for="passwordVerify">Verify your Password:
        </label>
        <input type="password" name="passwordVerify" placeholder="Enter in your Password again" />
        <span id="errorPasswordVerify"></span><br>

        <label for="firstName">First Name:
        </label>
        <input type="text" name="firstName" placeholder="Enter your First Name" />
        <span id="errorFirstName"></span><br>

        <label for="lastName">Last Name:
        </label>
        <input type="text" name="lastName" placeholder="Enter your Last Name" />
        <span id="errorLastName"></span><br>

        <label for="email">Email:
        </label>
        <input type="text" name="email" placeholder="Enter your Email Address" />
        <span id="errorEmail"></span><br>

        <label for="phoneNumber">Phone Number
        </label>
        <input type="text" name="lastName" placeholder="Enter your Phone Number" />
        <span id="errorPhoneNumber"></span><br>

        <label for="signUpNewsletter">Sign up for newsletter:
        </label>
          <input type="radio" name="signUpNewsletter" value="Yes" checked> Yes 
            <input type="radio" name="signUpNewsletter" value="No"> No
            <span id="errorSignUp"></span><br>
        <input type="submit" value="Next step" >
    </form>
</section>
Ramesh Navi
  • 773
  • 1
  • 11
  • 24
  • Downvoted because of using only `return false` will still submit the form. You need to use `event.preventDefault()`; in order to prevent submissions and show frontend validations – Abdulla Nilam Jun 10 '19 at 05:02
  • Depending on his logic he has to return true or false. – Ramesh Navi Jun 10 '19 at 05:08
  • what you mean by `return true or false` ?? – Abdulla Nilam Jun 10 '19 at 05:09
  • The function has to return `true` if the form inputs are valid and let the form be submitted or return `false` if input fields are invalid. – Ramesh Navi Jun 10 '19 at 05:15
  • You still didn't get what I'm saying. read this https://stackoverflow.com/a/3350351/4595675 – Abdulla Nilam Jun 10 '19 at 05:16
  • The question was "JavaScript Form Validation Not Returning False" I showed him it returns false and letting him work on his logic and how can validate input data. – Ramesh Navi Jun 10 '19 at 05:17
  • when you're answering here, you should know what was OP trying to do. So based on that you have to write what's wrong and what should be done. Don't stick to point what OP asking. Its useless then, being here – Abdulla Nilam Jun 10 '19 at 05:19
  • All personal beliefs!! I Beleive showing right path instead of spoon feeding. – Ramesh Navi Jun 10 '19 at 05:24
  • @AbdullaNilam The form is not submitted if an inline submit event handler returns `false`. Notice the structure: `onsubmit="return checkAll();"`, this won't need any separate default action preventing, since this is a default action preventing itself. Please see [an example](https://jsfiddle.net/by1xtvdz/). – Teemu Jun 10 '19 at 08:57