'2 functions in the same button during submitting the form

I want to submit a form , the story that what i want exactly is when i click on the button submit" if there was an error(fields not filled) , show me message and go to a specific "id" section in the body otherwise it will jump automatically to new section tag,

I have applied that on a the button submit like that :

  <script>
    function validate() {
    if (form.errors){
      return alert('{{ message }}');
      document.getElementById('firstsectionid').scrollIntoView();
    } 
    else{
      document.getElementById('nextsectionid').scrollIntoView();
}

   </script>

code button submit:

  <button class="floated" href='#nextsectionid' onclick="validate()">Next</button>

But it doesn't give any result , help please. thanks in advance.



Solution 1:[1]

  <script>
    function validate() {
    if (form.errors){
      return alert('{{ message }}');
      // the line below will never run with a 'return' above it.
      document.getElementById('firstsectionid').scrollIntoView();
    } 
    else{
      document.getElementById('nextsectionid').scrollIntoView();
}

   </script>

try this

  <script>
    function validate() {
    if (form.errors){
      document.getElementById('firstsectionid').scrollIntoView();
      return alert('{{ message }}');
    } 
    else{
      document.getElementById('nextsectionid').scrollIntoView();
}

   </script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex