简体   繁体   中英

Can't get JavaScript method to validate my HTML form

I'm trying to use a javascript method to validate my form but it doesn't seem to be working. No dialog box pops up warning me of any errors even if an empty form is submitted. What could be the error? (Please Note: The JS File has a method defined for a time-stamp that I am currently not using in my form tag. I need some help with calling two functions as well.)

Here's the code:

 function setDate() { document.getElementById('date').value = new Date(); } function validateForm() { var a = document.getElementById('name').value; var b = document.getElementById("contact1").value; var blen = b.length; var c = document.getElementById("address1").value; var d = document.getElementById("stblimit").value; var dlen = d.length; var e = document.getElementById("creditlimit").value; var f = document.getElementById("commission").value; var g = document.getElementById("servicecharges").value; //DATE var h = document.forms["addRetailer"]["date"].value; if (a == null || a == "") { alert("Name must be filled out"); return false; } else if (b == null || b == "" || blen == 0 || blen > 10 || blen < 10) { alert("Enter a valid number"); return false; } else if (c == null || c == "") { alert("Primary Address must be filled out"); return false; } else if (d == null || d == "" || dlen == 0 || dlen < 0) { alert("Set Box Top Limit must be filled with a valid number"); return false; } else if (e == null || e == "") { alert("Credit Limit must be filled out"); return false; } else if (f == null || f == "") { alert("Commission Percentage must be filled out"); return false; } else if (g == null || g == "") { alert("Service Charges must be filled out"); return false; } } 
 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <script src="formvalidation.js" type="text/javascript"></script> <title>Register Retailer</title> </head> <body> <h1>Retailer Information</h1> <form name="addRetailer" action="RetailerController" method="post" onsubmit="return validateForm()"> <table> <tr> <td>Name</td> <td> <input type="text" name="name" id="name"></input> </td> </tr> <tr> <td>Contact Number 1</td> <td> <input type="text" name="contact1" id="contact1"></input> </td> </tr> <tr> <td>Contact Number 2</td> <td> <input type="text" name="contact2" id="contact2"></input> </td> </tr> <tr> <td>Address Line 1</td> <td> <input type="text" name="address1" id="address1"></input> </td> </tr> <tr> <td>Address Line 2</td> <td> <input type="text" name="address2" id="address2"></input> </td> </tr> <tr> <td>City</td> <td> <input type="text" name="city" id="city"></input> </td> </tr> <tr> <td>State</td> <td> <input type="text" name="state" id="state"></input> </td> </tr> <tr> <td>Pin Code</td> <td> <input type="text" name="pin" id="pin"></input> </td> </tr> <tr> <td>Set Top Box Limit</td> <td> <input type="text" name="stblimit" id="stblimit" value="0"></input> </td> </tr> <tr> <td>Credit Limit</td> <td> <input type="text" name="creditlimit" id="creditlimit"></input> </td> </tr> <tr> <td>Commission Percentage</td> <td> <input type="text" name="commission" id="commission" value="0.0"></input> </td> </tr> <tr> <td>Service Charges</td> <td> <input type="text" name="servicecharges" id="servicecharges"></input> </td> </tr> <tr> <td>Date of Registration</td> <td> <input type="text" name="date" id="date"></input> </td> </tr> <tr> <td>&nbsp;</td> <td> <input type="hidden" value="registerCustomer" name="action"></input> <input type="submit" value="Register"></input> </td> </tr> </table> </form> <br> <br>Click <a href="mainPage.html"> Home </a>To Return To The Main Screen </body> </html> 

EDIT:

Here is a screenshot of my Eclipse IDE workspace. My js file and html file aren't in the same sub-folder, although they are under 'Web Content'. Could that be the issue?

Screenshot of Eclipse IDE Workspace

Using Chrome, it seems to work for me:

在此处输入图片说明

The only thing that I changed is that I removed the link to the external JS file. It is possible the error is there and preventing your code from running. Check that file.

You may not have linked your JS file properly.

Looking at your screenshot I noticed you're using Tomcat and Java EE, which follows Unix style syntax for it's directory on the web server.

Your directory:

-Webcontent/
    -WEB-INF/
        +addRetailer
    -JavaScript/
        +validateForm.js

So, your HTML file is in WEB-INF which is under Webcontent and the form validator is under javascript, also under webcontent.

There's three solutions I have for this:

  1. Move the JavaScript folder into WEB-INF and change the script reference to: "JavaScript/formvalidation.js"
  2. Change the script reference to jump 'up' a directory layer using the double-dot'..' which should end up being: "../JavaScript/formvalidator.js"
  3. Use HTML5's form validation instead, which doesn't need JavaScript. and is much neater. You can find details on the Mozilla Dev Network here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM