0
votes

I have a form spread across two pages. I'm trying to validate the email address but whenever I put in a bad email I get an error in the console that says an invalid form control is not focusable

The easiest way to test the snippet:

  1. LEAVE FIRST AND LAST NAME BLANK
  2. ENTER A SINGLE LETTER FOR EMAIL ADDRESS
  3. CLICK ON NEXT
  4. CLICK ON SUBMIT
  5. CHECK THE CONSOLE FOR ERROR MESSAGE

This stackoverflow answer not focusable Does not answer the question. The email input does not have "REQUIRED" and all buttons have type='BUTTON' except the submit but that has a click function associated with it.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://www.sustainablewestonma.org/swag/public/css/getInvolved001.css?test1=apple">
        <div id='container'>
            <div id='section-container'>
                <div id='section1' class='sections'>
                    <div id='section1b'>
                    <span class='majCap'>Get Involved</span>
                    <span class='minCap'>Enter your details below</span>
                    <form action='getInvolvedCtrl.php' method='POST' id='myForm' onsubmit="return mySubmit(event)">
                        <div class='columns'>
                            <input class='detailsC' type="text"  name="fname" placeholder="First Name" id='fname'>
                            <input class='detailsC' type="text"  name="lname" placeholder="Last Name"  id='lname'>
                            <input class='detailsC' type="email" name="email" placeholder="Email..."   id='email'>
                        </div>
                        <div class='rowsDet'>
                            <span class='detailsR'><input  type="checkbox" name="wres">Weston Resident</span>
                            <span class='detailsR'><input  type="checkbox" name="news">Quarterly Newsletter</span>
                        </div>
                        <div class='spacer'></div>
                        <span class="minCap" >Areas of Interests</span>
                        <div class='rowsInt'>
                            <div class="columns">
                                <span><input type="checkbox" name="energy-int">Energy</span>
                                <span><input type="checkbox" name="recycling-int">Recycling</span>
                                <span><input type="checkbox" name="composting-int">Composting</span>
                                <span><input type="checkbox" name="transport-int">Transportation</span>
                            </div>
                            <div class="columns">
                                <span><input type="checkbox" name="climate-int">Climate</span>
                                <span><input type="checkbox" name="trees-int">Trees</span>
                                <span><input type="checkbox" name="pollinators-int">Pollinators</span>
                                <span><input type="checkbox" name="water-int">Water</span>
                                <span><input type="checkbox" name="other-int">Other</span>
                            </div>
                        </div>
                        <button id='next' type='button' onclick="nextPage()">next</button>
                    </div>    
                <div id='section2' class='sections'>
                    <span class='minCap' >Current Campaign Information:</span>
                        <div class='rowsCam'>
                            <div class="columns">
                                <span><input type="checkbox" name="cleanup-cpg">Town Cleanup</span>
                                <span><input type="checkbox" name="pollinators-cpg">Pollinators</span>
                            </div>
                            <div class="columns">
                                <span><input type="checkbox" name="gas-cpg">Gas Leaks</span>
                                <span><input type="checkbox" name="cca-cpg">CCA</span>
                            </div>
                        </div>
                        <div class='spacer2'></div>
                            <input type="submit" value="SUBMIT">
                            <button id='back' type='button' onclick="backPage()">back</button>
                    </form>
                </div>
                    
                </div>
                
            </div>
        </div>
        <script>
           
        
            function nextPage(){
               
                var section1b = document.getElementById('section1b');
                section1b.style.display='none';
                var section2 = document.getElementById('section2');
                section2.style.display='block';
            }
            function backPage(){
                var section1b = document.getElementById('section1b');
                section1b.style.display='block';
                var section2 = document.getElementById('section2');
                section2.style.display='none';
            }
            function mySubmit(x){
                event.preventDefault();
                
                var fname = document.getElementById('fname').value;
                var lname = document.getElementById('lname').value;
                var email = document.getElementById('email').value;
                
                
                var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
                if(!email.match(mailformat))email="";
                
                if(fname == "" || lname == "" || email == ""){
                    alert("Please enter name and email");
                    backPage();
                    return false;
                }else{
                
                    //var myForm = document.getElementById('myForm');
                    //myForm.submit();
                    return true;
                }
            }
            
        </script>
    </body>
</html>
1

1 Answers

1
votes

You have to change just one line of code :-

you have to set **<input type="text" />** instead of <input type="email" />

so, the updated line will be :-

<input class='detailsC' type="text" name="email" placeholder="Email..."   id='email'>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://www.sustainablewestonma.org/swag/public/css/getInvolved001.css?test1=apple">
<div id='container'>
  <div id='section-container'>
    <div id='section1' class='sections'>
      <div id='section1b'>
        <span class='majCap'>Get Involved</span>
        <span class='minCap'>Enter your details below</span>
        <form action='getInvolvedCtrl.php' method='POST' id='myForm' onsubmit="return mySubmit(event)">
          <div class='columns'>
            <input class='detailsC' type="text" name="fname" placeholder="First Name" id='fname'>
            <input class='detailsC' type="text" name="lname" placeholder="Last Name" id='lname'>
            <input class='detailsC' type="text" name="email" placeholder="Email..." id='email'>
          </div>
          <div class='rowsDet'>
            <span class='detailsR'><input type="checkbox" name="wres">Weston Resident</span>
            <span class='detailsR'><input type="checkbox" name="news">Quarterly Newsletter</span>
          </div>
          <div class='spacer'></div>
          <span class="minCap">Areas of Interests</span>
          <div class='rowsInt'>
            <div class="columns">
              <span><input type="checkbox" name="energy-int">Energy</span>
              <span><input type="checkbox" name="recycling-int">Recycling</span>
              <span><input type="checkbox" name="composting-int">Composting</span>
              <span><input type="checkbox" name="transport-int">Transportation</span>
            </div>
            <div class="columns">
              <span><input type="checkbox" name="climate-int">Climate</span>
              <span><input type="checkbox" name="trees-int">Trees</span>
              <span><input type="checkbox" name="pollinators-int">Pollinators</span>
              <span><input type="checkbox" name="water-int">Water</span>
              <span><input type="checkbox" name="other-int">Other</span>
            </div>
          </div>
          <button id='next' type='button' onclick="nextPage()">next</button>
      </div>
      <div id='section2' class='sections'>
        <span class='minCap'>Current Campaign Information:</span>
        <div class='rowsCam'>
          <div class="columns">
            <span><input type="checkbox" name="cleanup-cpg">Town Cleanup</span>
            <span><input type="checkbox" name="pollinators-cpg">Pollinators</span>
          </div>
          <div class="columns">
            <span><input type="checkbox" name="gas-cpg">Gas Leaks</span>
            <span><input type="checkbox" name="cca-cpg">CCA</span>
          </div>
        </div>
        <div class='spacer2'></div>
        <input type="submit" value="SUBMIT">
        <button id='back' type='button' onclick="backPage()">back</button>
        </form>
      </div>

    </div>

  </div>
</div>
<script>
  function nextPage() {
    var section1b = document.getElementById('section1b');
    section1b.style.display = 'none';
    var section2 = document.getElementById('section2');
    section2.style.display = 'block';
  }

  function backPage() {
    var section1b = document.getElementById('section1b');
    section1b.style.display = 'block';
    var section2 = document.getElementById('section2');
    section2.style.display = 'none';
  }

  function mySubmit(x) {
    event.preventDefault();
    var fname = document.getElementById('fname').value;
    var lname = document.getElementById('lname').value;
    var email = document.getElementById('email').value;
    var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    if (!email.match(mailformat)) email = "";
    if (fname == "" || lname == "" || email == "") {
      alert("Please enter name and email");
      backPage();
      return false;
    } else {
      //var myForm = document.getElementById('myForm');
      //myForm.submit();
      return true;
    }
  }
</script>
</body>

</html>

can also check my solution here -

https://codepen.io/pen/?editors=1010