0

I'm trying to do a form and while the alert is popping up it is still submitting. How do I get it to stop submitting??

function validate() {
  var first = document.register.first.value;
  if (first == "") {
    alert("please enter your name");
    first.focus();
    return false;
  }
  return (true);
}
<body>
  <form name="register" action="testform.php" onsubmit="return(validate());">
    <input type="text" name="first" />
    <button type="submit" />Submit
  </form>
</body>

4 Answers 4

1

You added the parenthesis on return() then return(validate()) which we use () when calling the function so it might be considering return a custom function which returns undefined and when returned the undefined it ignores and continue the execution. How ever the validate is called but it's response is not returned to the form.

Fixed version:

<head>
<script>
function validate(e) {
   var first = document.register.first.value;
   console.log(document.register.first)
    if( first == "" ) {
        alert( "please enter your name" ) ;
        return false;
    }
return(true);
}
</script>
</head>
<body>
    <form name="register" action="testform.php" onsubmit="return validate()">
        <input type="text" name="first" />
        <button type="submit" >sbmit</button>
    </form>
</body>
Sign up to request clarification or add additional context in comments.

Comments

0

You are better of using the required attribute on the front end of things. It will 'force' the user to input text into the input field before it is able to submit. Please note that I put quotation marks around the word 'force', because one can just edit the HTML and circumvent the HTML required attribute. Therefore make absolutely sure that you are validating user input on the PHP side as well.

Many tutorials and examples exist for PHP Form Validation, such as this one from W3Schools and this one from Medium.

<form name="register" action="testform.php">
  <input type="text" name="first" required/>
  <input type="submit" value="Submit"/>
</form>

2 Comments

Thank you ive got a bit of experience with php so i'll try your suggestion
@Kra9 If you follow one of the tutorials I provided you will be absolutely fine. They explain the necessary things and include the HTML as well.
0

You have several bugs in your code.

  1. <button> element is not self-closing
  2. you are calling focus on value of the input instead of the input element which throws exception

function validate() {
   var input = document.register.first;
   var text = input.value;   
    if( text == "" ) {
        alert( "please enter your name" ) ;
        input.focus();
        return false;
    }
    return true;
}

Comments

0

I think the issue is with the button's type="submit". Try changing it to type="button", with an onclick function that submits your form if validate() returns true.

edit: Arjan makes a good point, and you should use required. But this answers why the form was submitting.

3 Comments

type="submit"specifies that the button will submit the form it's associated with. You need to override this default behavior to do the validation, and then tell it to submit.
Why reinvent the wheel? Simply use required as its way easier. You will have to verify user input on the server side anyways.
Yes, you make a good point, and he should use required. But he did ask why his form was submitting onclick before checking his validate() function. It's also good to know in case he ends up in a situation where the simple required validation isn't enough (e.g., field A is required if radio B is selected).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.