aspose file tools*
The moose likes Struts and the fly likes JavaScript Validation in struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "JavaScript Validation in struts" Watch "JavaScript Validation in struts" New topic
Author

JavaScript Validation in struts

pankaj semwal
Ranch Hand

Joined: Oct 07, 2008
Posts: 300
Hi List[],

I am doing java script validation in struts .I am not
using validator frame work.And i want to do validation using java script without using validator frame work of struts.But when i run the page on browse its not validationg the page.
Please help me.
My Code.

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

<html>
<head>
<title>JSP for StudRegForm form</title>
</head>
<body>
<script type="text/javascript">

<!-- hide script from older browsers
function validateForm(studRegForm)
{

if(""==document.forms.contact.fname.value)
{
alert("Please enter your Father name.");
return false;
}

if(""==document.forms.contact.address.value)
{
alert("Please enter your address.");
return false;
}

if(""==document.forms.contact.dob.value)
{
alert("Please enter your Date of Birth .");
return false;
}
if(""==document.forms.contact.currentclass.value)
{
alert("Please enter Current class.");
return false;
}
if(""==document.forms.contact.name.value)
{
alert("Please enter Name.");
return false;
}
}
stop hiding script -->






</script>



<html:form attribute="studRegForm" action="/studReg" onsubmit = "return validateForm(studRegForm);">

<table size=10 border=1>
FatherName : <html:text name="fname" property="fname"/> <br/>
Address : <html:text name="address" property="address"/> <br/>
Date Of Birth : <html:text name="dob" property="dob"/> <br/>
Current Class : <html:text name="currentclass" property="currentclass"/> <br/>
You Name : <html:text name="name" property="name"/> <br/>
</table>
<html:submit/><html:cancel/>
</html:form>
</body>

</html>
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

This isn't Struts-related, but JavaScript and DOM.

1) You need to use the DOM form element.
2) You need to use the form values from that form element, the JavaScript you show has something called "contact" in it that seems to appear out of nowhere.

I'd recommend searching the web for "javascript form validation".

Dave
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JavaScript Validation in struts