Prevent form submission using Flask

  flask, html, javascript, python

Following the code from here, I wish to prevent a form from being submit which is running within Flask on a html page, the notification works but the form submits regardless.

JS:

 function ValidateIPaddress(inputText)
 {
 var ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
 if(inputText.value.match(ipformat))
 {
 document.ip_checks.IPs.focus();
 return true;
 }
 else
 {
 alert("You have entered an invalid IP address!");
 document.ip_checks.IPs.focus();return false;
 }
 }  

HTML:

<form name="ip_checks" method=post enctype=multipart/form-data >
      <p><label for="ipaddresses" class="form-label">IP Addresses:</label>
      <textarea name="IPs" cols="40" rows="2" class="form-control"></textarea></p>
      <p><input type = "submit" value = "Submit" class = "btn btn-primary" onclick="ValidateIPaddress(document.ip_checks.IPs)"/></p>
    </form>

I’ve attempted to use preventDefault() to no avail. Is there a way to do it with Flask?

Source: Ask Javascript Questions

LEAVE A COMMENT