-1

Hey guys really basic issue here I'm having trouble with. This is the first time I have coded validations from scratch and can't figure out why they are not working. When I click my submit button it just forward to "your file was not found" page. It is not acknowledging my validators at all. Input on validators in general, and how to improve or fix this would be much appreciated.

<DOCTYPE!>
<html>
  <head>
      <script src="gen_validatorv4.js" type="text/javascript"></script>

      <link rel="stylesheet" type="text/css" href="style.css">

      <body>
        <div id= "videoGameCircuit">
          <img src="Images/vg.jpg" width="900" height="400" alt="video game circuit" />
        </div>
        <div>
            <form action="myForm" onsubmit="return validateForm()" method="post">
            Name: <input type="text" name="name" value="" size="25" maxlength="50" /><br>
            Email: <input type="text" name="email" value="" size="25" maxlength="50" /><br>
            <input type="submit" name="submit" value="sign me up!" />
          </form>
          <script type="text/javascript">
          var frmvalidator  = new Validator("myForm");
          //where myform is the name/id of your form

          var ele = myForm; ele.addEventListener("submit", function(e){e.preventDefault();
      

          frmvalidator.EnableOnPageErrorDisplaySingleBox();
          frmvalidator.EnableMsgsTogether();


           frmvalidator.addValidation("name","req","Please enter your name");
           frmvalidator.addValidation("name","maxlen=20", "Max length for name is 20");
           frmvalidator.addValidation("name","alpha_s","Name can contain alphabetic chars only");
          </script>
        </div>
      </body>
  </head>
</html>

1 Answers1

0

In your case you will need to stop default submiting of form before doing validation.

So in your case something like this.

var frmvalidator  = new Validator("myForm");
      //where myform is the name/id of your form
      frmvalidator.EnableOnPageErrorDisplaySingleBox();
      frmvalidator.EnableMsgsTogether();
      frmvalidator.addValidation("name","req","Please enter your name");
      frmvalidator.addValidation("name","maxlen=20",    "Max length for name is 20");
      frmvalidator.addValidation("name","alpha_s","Name can contain alphabetic chars only");
//add id to form like id="myForm"
document.querySelector("#myForm").addEventListener("submit", function(e){
    e.preventDefault();    //stop form from submitting
    //run your validation code
    var valid = frmvalidator.valid();//your custom validation method
    if(!valid){
       //alert to user
    }
    else {
      //submit form
      e.submit();
    }
});

Hope this helps.

Mykola Borysyuk
  • 3,373
  • 1
  • 18
  • 24
  • No luck. Thanks though. – Josh Franker Oct 10 '16 at 15:35
  • You sure you clear onsubmit="return validateForm()" and add id to form? – Mykola Borysyuk Oct 10 '16 at 15:37
  • I have no idea how to do that. this is literally the first morning I have ever looked at custom event handling in web development. – Josh Franker Oct 10 '16 at 15:38
  • Just remove it from HTML...idk how else i can help((( Sorry but you have to look at those tutorials. And check about default html behaviour before going next. – Mykola Borysyuk Oct 10 '16 at 15:42
  • You need to add into
    and from that tag remove onsubmit="return validateForm()". If you add my code inside your script tag. document.querySelector("#myForm").addEventListener should catch your form submition and if you add console.log() there you will see it. Then go step by step And you will get it.
    – Mykola Borysyuk Oct 10 '16 at 15:46