Validate form on submit event

  • Thread starter Thread starter sergio.leon.leon
  • Start date Start date
S

sergio.leon.leon

Hello:
I hope that my english will be readable, I have expended a lot of time
for writing this message ;-)

I have been using the validation controls in several applications.
After this I have not found a control for validating the form as a
whole and during the onsubmit event. I remember that in classic ASP I
did this without much effort. But now, I have tried with property
ValidationGroup, control CustomValidator or ValidationSummary, but I
always have a partial resolution for this problem. Perhaps, with
Javascript but finally I have problems with AJAX and other built-in
features.

I want to validate the controls only during the onsubmit event, not
before.

Anyone know a server control or similar that lets do this in a simple
way, and without problems with the rest of the ASP.NET?

Thanks.
 
Sergio,

If I understand, you want to stop individual control validation when moving
from one control to the other. And you also want to validate a certain group
of controls on a page.

To stop validation when moving from one control to another the easiest way
to do so will be to turn off client side validation and only validate server
side. Do this by setting EnableClientScript to False.

To group controls for validation so that you can have separate validation
when two different buttons are clicked set the ValidationGroup to the same
string value for all validators and the button you want to validate on the
same button click.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
Hi Justin:

Finally, I think that I found a simple solution for this. I hope that
it will be a good solution.

1. Don't use validator controls for any controls.
2. Only use a CustomValidator control. Furthermore, it will be a
novisible control (with top: -500px for example).
3. Do the entire validation in the code of this last validator
control.

Something like this:

<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="Default4.aspx.vb" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<script language="javascript" type="text/javascript">
function validacionFormulario(sender, e) {
//debugger
var s = "";
var r = true;
if (!($get("<%=TextBox1.ClientID%>").value)) {
s += "TextBox1 is required.\n";
r = false;
}
if (!($get("<%=TextBox2.ClientID%>").value)) {
s += "TextBox2 is required.\n";
r = false;
}
if (!($get("<%=TextBox3.ClientID%>").value)) {
s += "TextBox3 is required.\n";
r = false;
}
if (!($get("<%=TextBox4.ClientID%>").value)) {
s += "TextBox4 is required.\n";
r = false;
}
e.IsValid = r;
if (!r)
alert(s);
}
</script>

<style type="text/css">
.dummy
{
position: absolute;
top: -5000px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox5" runat="server" ReadOnly="true"
CssClass="dummy"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox5"
Display="None" ErrorMessage="CustomValidator"
ClientValidationFunction="validacionFormulario"
SetFocusOnError="false" ValidateEmptyText="true"></
asp:CustomValidator>
</div>
</form>
</body>
</html>
 
Back
Top