I have a JSP( a form) that calls a servlet when I click the submit button. I want to add javascript validation to the form fields. When I try to embed the script it does not run. I tried to link the file to the JSP, but that also did not work. My Javascript works well with just the HTML page. Is it possible to call the js file from within the JSP before the servlet is called?

Recommended Answers

If you want validation in the servlet, then use java code after you get the request.
But that is not very good because you have already sent the request. The idea is to validate before sending the request at the jsp.

So you can try this:


       
Jump to Post

All 3 Replies

If you want validation in the servlet, then use java code after you get the request.
But that is not very good because you have already sent the request. The idea is to validate before sending the request at the jsp.

So you can try this:

<form name="..." action="... " onsubmit="return validateMethod();">

</form>

The validateMethod must return true or false. If it returns true the form is submitted.

If you want validation in the servlet, then use java code after you get the request.
But that is not very good because you have already sent the request. The idea is to validate before sending the request at the jsp.

So you can try this:

<form name="..." action="... " onsubmit="return validateMethod();">

</form>

The validateMethod must return true or false. If it returns true the form is submitted.

I tried your suggestion, however I got the same previous results. Here is my JSP code and JavaScript code. Can you pls suggest where in the code modification has to be done?
Thanks

JSP Code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Enter Employee Information  </title>


</head>
<body>
<table cellspacing="5" border="0">

<form action = "UpdateDBServlet" method = "post">

<tr>
	<td align="right">EmpID:</td>
	<td><input type="text" name="empID"></td>
</tr>
<tr>
	<td align="right">First Name:</td>
	<td><input type="text" name="firstName"></td>
</tr>
<tr>
	<td align="right">Last Name:</td>
	<td><input type="text" name="lastName"></td>
</tr>
<tr>
	<td align="right">Address:</td>
	<td><input type="text" name="address"></td>
</tr>
<tr>
	<td align="right">Designation:</td>
	<td><input type="text" name="designation"></td>
</tr>
<tr>
	<td align="right">Salary:</td>
	<td><input type="text" name="salary"></td>
</tr>
<tr>
	<td align="right">Work Phone:</td>
	<td><input type="text" name="workPhone"></td>
</tr>
<tr>
	<td align="right">Home Phone:</td>
	<td><input type="text" name="homePhone"></td>
</tr>

</table>

<input type = "submit" value = "Submit" > 
</form>

</body>
</html>

JavaScript Code

var $ = function (id) {
	return document.getElementById(id);
	}
	
	
var submit_click = function () {
	var empID = parseFloat($("empID").value);
	var salary = parseFloat($("salary").value);
	
	$("empID").value = "";
	$("salary").value = "";
	
	if(isNaN(empID) || empID <0){
		alert("Employee ID must be a number that is zero or more!");
	} else if (isNaN(salary) || salary <0) {
		alert("Salary must be a number that is zero or more!");
	} 
			
}

window.onload = function () {
	$("submit").onclick = submit_click;
	
	}

You haven't defined an id attribute at your fields in the form, you haven't "imported" your javascript file in the jsp file, and the onsubmit event must return true or false.
http://www.w3schools.com/default.asp

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.