Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation problem with multiple submit buttons on single form

Status
Not open for further replies.

celia05es

Programmer
Jan 10, 2002
81
US
Hello,
I have a form with several fields.
Each field can be modified:
- individually (one submit button next to the field)
- globally (a submit button at the end of the form).

Before submitting the form, a validation needs to be performed.... so I added: onSubmit="return isValid()".

If the validation does not pass, the form needs to be shown with the data previously entered by the user.

Now, I haven't even come to the previous problem I have mentioned..... my problem is that if a field is empty, the function isValid() recognize it (message sent) .... but the reDirect function is performed!!!!

Could you help please?


I am using a javascript function "reDirect" to perform the multiple submit button:

- target = 0 => Individual submit
= 1 => Global submit
- fieldName => Fieldname to be modified
- index => Field number of fieldName.

I have two global variables: fieldIndex and modifType, so the validation function will know whith variable to look at (with fieldIndex) and the modifType ( 0=> individual, 1 => global)

The scriptlet is :
==================================
<html>
<title>Test</title>
<head>
<script language=JavaScript>
<!--- oculta el script para navegadores antiguos

var fieldIndex;
var modifType;

function isValid()
{
var str = document.attributes.elements[fieldIndex].value;
if (str.match(/^(\s)*$/) )
{
alert("This field ield cannot be empty");
document.attributes.elements[fieldIndex].select();
document.attributes.elements[fieldIndex].focus();
return false;
}
}

function reDirect(target, fieldName,index)
{
fieldIndex=index;
modifType=target;

if(target == 0) document.attributes.action="modif_one.jsp?name=fieldName";
if(target == 1) document.attributes.action="modif_all.jsp";
document.attributes.submit();
}

// end hiding from old browsers -->
</script>
</head>
<%@page language="java" %>
<body>
<form onSubmit="return isValid()" method="post" name="attributes" action="">
<table>
<tbody>
<%
int fieldInd=0;
for (int i=0; i<5; i++)
{
String paramValue = "A"+String.valueOf(i);
String paramName = "Name"+String.valueOf(i);
%>
<tr>
<td>
<input type=text name=<%=paramName%> value=<%=paramValue%>>
<input type="image" onClick="reDirect(0,'<%=paramName%>',<%=String.valueOf(fie
ldInd)%>)" src="../images/M.gif">
</td>
</tr>
<%
fieldInd++;
}
%>
</tbody>
</table>
<input onclick=reDirect(1,'','','') type=button value="Modify all">
</form>
</body>
</html>
=====================================

 
See if the red/bolded portion fixes your problem (the rest just cuts down your lines of code)
Code:
function reDirect(target, fieldName,index) {
   fieldIndex = index;
   modifType = target;
   document.forms["attributes"].action = (target == 0) ? "modif_one.jsp?name=[b][COLOR=red]" + fieldName[/color][/b] : "modif_all.jsp";
   document.forms["attributes"].submit();
   [b][COLOR=red]return false;[/color][/b]
}

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
I am currently modifying the database I am working with so I cannot test it right now. Hopefully tonight things will work better and I will try your suggestion. Thanks!
Elisabeth
 
Well, it still does not work.
I think I might have solved my first problem: I don't perform the validation at the form level (with form .... onSubmit=return isValid()) since it does not seem to work.... instead I perform the validation inside the reDirect function... and thanks to your modification it works!!!
Now, I am facing the other problem.... I don't want the reload to be performed if the validation fails.

Something strange happens....
- I press an individual modif button (with a null entry)
The validation fails
A reload is performed
The previous values entered by the user are therefore lost. In addition, I can't focus on the field with the problem!

- I press the global modification (with a null entry)
The validation fails
a reload is not done !!!!!


I have simplified the jsp file so you can see by yourself and may be help me out!!!
The validation is always meant to fail since it always returns false... but you will see that in the individual case, a reload is performed and in the global case no reload is performed!!!

======================
<html>
<title>Test</title>
<head>
<script language=JavaScript>

<!--- oculta el script para navegadores antiguos
function isNotNull(num)
{
alert("individual validation - validation fails");
return false;
}

function formValid()
{
alert("Entering formValid => validation fails");
return false;
}

function reDirect(target, fieldName, fieldType,fieldInd)
{
alert("reDirect fieldName= "+fieldName+" type="+fieldType);

// Validacion
//-----------
if (target == 0)
{
if (!isNotNull(fieldInd)) return false;
}
else
{
if (!formValid()) return false;
}
document.forms["attributes"].action = (target == 0) ? "modif_one.jsp?name=" + fieldName : "modif_all.jsp";
document.forms["attributes"].submit();
}

// end hiding from old browsers -->
</script>
</head>
<%@page language="java" %>
<%@page import="util.*"%>
<hr>
<body onload="if (document.getElementById) document.getElementById('L1').focus();">
<P>
<%
try
{
int ind=1;
%>
<form onSubmit="return true" method="post" name="attributes" action="">
<table>
<tbody>
<%
int fieldInd=0;
for (int i=0; i<2; i++)
{
String id="";
if (i == 0)
id="id=\"L1\"";
String paramName = "Field"+i;
String paramValue = "3";
String paramLabel= "Label"+i;
%>
<tr>
<td><%=paramLabel%>: </td>
<td>
<input type=text name=<%=paramName%> <%=id%> tabindex=<%=ind%> size=1 value=<%=paramValue%>>
<input type="image" onClick="reDirect(0,'<%=paramName%>','CHAR','<%=fieldInd%>')" src="../images/M.gif">
<%
ind=ind++;
fieldInd++;
%>
</td>
</tr>
<%
} // fin bucle
%>
</tbody>
</table>
</form>
<input onclick=reDirect(1,'','','') type=button tabindex=<%=ind%> onBlur=document.getElementById('L1').focus() value="Modificar todos">
<script language="javaScript">document.getElementById('L1').focus();</script>
<%
}
catch (Exception e) {}
%>
</body>
</html>

 
I have solved the problem. Thanks for your help.
ELisabeth
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top