Hi - I have a form with 10 or so fields in it - each with a required field validator. The text for each validitor is "Required".
I then have a validation summary - if the user has not completed the form it should just say "Please complete all required fields" - but it doesn't, it says "Please complete all required fields Required Required Required Required"
So it's actually listing all the fields that need completing but I don't want it to do that as the field is just "Required".
How do I get it to just say "Please complete all required fields"? Regardless of how many fields need completing.
How do I get it to just say "Please complete all required fields"? Regardless of how many fields need completing.
Then whats the point in using ValidationSummary control, do you know what it does, it simply format the error message as per the display mode and in the end you get a alert or filled DIV with innerHTML.
So if you don't want to show all error message then it's simply a error message you can show yourself in alert look at example here: (SanSan: I am using your code to demonstrate), if you understand this simply drop the ValidationSummaryControl and Give the
javascript function a hardcoded text.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" 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 type="text/javascript" language="javascript">
function ValidateAndShowSummary() {
if (Page_ClientValidate() == false) {
if (Page_IsValid == false) {
//instead of this you can simply alert a hardcoded error message here....but you make you clear
//I am using this..later on you can drop validation summary control.
var errorMessage = document.getElementById("ValidationSummary1").headertext.toString();
alert(errorMessage);
// if you want to show div instead of, add a div set its display to none and here you can set div's innerHTML with above error message
// and set display to block.
return false;
}
return false;
}
return true
}
</script>
</head>
<body>
<form id="Form1" runat="server">
<table>
<tr>
<td>
<table bgcolor="b0c4de" cellspacing="10">
<tr>
<td align="right">
first Name:
</td>
<td>
<asp:TextBox ID="txt_name" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txt_name"
SetFocusOnError="true" ErrorMessage="Required" Text="" runat="server" Display="Dynamic" />
</td>
</tr>
<tr>
<td align="right">
last name:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="TextBox1"
SetFocusOnError="true" ErrorMessage="Required" Text="" runat="server" Display="Dynamic" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="b1" Text="Submit" runat="server" CausesValidation="false" OnClientClick="JavaScript: return ValidateAndShowSummary();" />
</td>
<td>
</td>
</tr>
</table>
</td>
</tr>
</table>
<br />
<asp:ValidationSummary ID="ValidationSummary1" ShowMessageBox="false" HeaderText="You must enter values in the required fields."
EnableClientScript="true" runat="server" DisplayMode="List" ShowSummary="False" />
</form>
</body>
</html>
loydall
Participant
975 Points
366 Posts
ok - easy/stupid question time - validation control
Jul 16, 2010 02:15 PM|LINK
Hi - I have a form with 10 or so fields in it - each with a required field validator. The text for each validitor is "Required".
I then have a validation summary - if the user has not completed the form it should just say "Please complete all required fields" - but it doesn't, it says "Please complete all required fields Required Required Required Required"
So it's actually listing all the fields that need completing but I don't want it to do that as the field is just "Required".
How do I get it to just say "Please complete all required fields"? Regardless of how many fields need completing.
Cheers.
sansan
All-Star
53942 Points
8147 Posts
Re: ok - easy/stupid question time - validation control
Jul 16, 2010 06:01 PM|LINK
Set the Error Message to blank and set Text to Required for all validators.
When validationsummary is invoked, it will display the error Messages
so set the error messaget to ""
check this
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txt_name" SetFocusOnError="true" ErrorMessage="" Text="Required" runat="server" Display="Dynamic" />set Display to dynamic so that it will display only when the textbox text is blank
hope this is clear
sansan
All-Star
53942 Points
8147 Posts
Re: ok - easy/stupid question time - validation control
Jul 16, 2010 06:09 PM|LINK
check this sample:
<html> <body> <form id="Form1" runat="server"> <table> <tr> <td> <table bgcolor="#b0c4de" cellspacing="10"> <tr> <td align="right"> first Name: </td> <td> <asp:TextBox ID="txt_name" runat="server" /> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txt_name" SetFocusOnError="true" ErrorMessage="" Text="Required" runat="server" /> </td> </tr> <tr> <td align="right"> last name: </td> <td> <asp:TextBox ID="TextBox1" runat="server" /> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="TextBox1" SetFocusOnError="true" ErrorMessage="" Text="Required" runat="server" /> </td> </tr> <tr> <td> </td> <td> <asp:Button ID="b1" Text="Submit" runat="server" /> </td> <td> </td> </tr> </table> </td> </tr> </table> <br /> <asp:ValidationSummary ID="ValidationSummary1" ShowSummary="false" ShowMessageBox="true" HeaderText="You must enter values in the required fields:" EnableClientScript="true" runat="server" /> </form> </body> </html>[url=http://www.freeimagehosting.net/][img]http://www.freeimagehosting.net/uploads/0c9aef0131.gif[/img][/url]
SSA
Star
9368 Points
1576 Posts
Re: ok - easy/stupid question time - validation control
Jul 16, 2010 07:40 PM|LINK
Then whats the point in using ValidationSummary control, do you know what it does, it simply format the error message as per the display mode and in the end you get a alert or filled DIV with innerHTML.
So if you don't want to show all error message then it's simply a error message you can show yourself in alert look at example here: (SanSan: I am using your code to demonstrate), if you understand this simply drop the ValidationSummaryControl and Give the javascript function a hardcoded text.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" 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 type="text/javascript" language="javascript"> function ValidateAndShowSummary() { if (Page_ClientValidate() == false) { if (Page_IsValid == false) { //instead of this you can simply alert a hardcoded error message here....but you make you clear //I am using this..later on you can drop validation summary control. var errorMessage = document.getElementById("ValidationSummary1").headertext.toString(); alert(errorMessage); // if you want to show div instead of, add a div set its display to none and here you can set div's innerHTML with above error message // and set display to block. return false; } return false; } return true } </script> </head> <body> <form id="Form1" runat="server"> <table> <tr> <td> <table bgcolor="b0c4de" cellspacing="10"> <tr> <td align="right"> first Name: </td> <td> <asp:TextBox ID="txt_name" runat="server" /> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txt_name" SetFocusOnError="true" ErrorMessage="Required" Text="" runat="server" Display="Dynamic" /> </td> </tr> <tr> <td align="right"> last name: </td> <td> <asp:TextBox ID="TextBox1" runat="server" /> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="TextBox1" SetFocusOnError="true" ErrorMessage="Required" Text="" runat="server" Display="Dynamic" /> </td> </tr> <tr> <td> </td> <td> <asp:Button ID="b1" Text="Submit" runat="server" CausesValidation="false" OnClientClick="JavaScript: return ValidateAndShowSummary();" /> </td> <td> </td> </tr> </table> </td> </tr> </table> <br /> <asp:ValidationSummary ID="ValidationSummary1" ShowMessageBox="false" HeaderText="You must enter values in the required fields." EnableClientScript="true" runat="server" DisplayMode="List" ShowSummary="False" /> </form> </body> </html>SSA
Star
9368 Points
1576 Posts
Re: ok - easy/stupid question time - validation control
Jul 16, 2010 07:49 PM|LINK
And here is clean example without using ValidationSummaryControl. Change variable showSummary value, if you want to see alert or box.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" 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 type="text/javascript" language="javascript"> var showSummary = true; function ValidateAndShowSummary() { if (Page_ClientValidate() == false) { var errorMessage = "Please enter all the fields required."; if (showSummary == false) { alert(errorMessage); } else { var box = document.getElementById("ErrorMessageBox"); box.innerHTML = "Please enter all the fields required."; box.style.display = "block"; } return false; } return true } </script> </head> <body> <form id="Form1" runat="server"> <table> <tr> <td> <table bgcolor="b0c4de" cellspacing="10"> <tr> <td align="right"> first Name: </td> <td> <asp:TextBox ID="txt_name" runat="server" /> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txt_name" SetFocusOnError="true" ErrorMessage="Required" Text="" runat="server" Display="Dynamic" /> </td> </tr> <tr> <td align="right"> last name: </td> <td> <asp:TextBox ID="TextBox1" runat="server" /> </td> <td> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="TextBox1" SetFocusOnError="true" ErrorMessage="Required" Text="" runat="server" Display="Dynamic" /> </td> </tr> <tr> <td> </td> <td> <asp:Button ID="b1" Text="Submit" runat="server" CausesValidation="false" OnClientClick="JavaScript: return ValidateAndShowSummary();" /> </td> <td> </td> </tr> </table> </td> </tr> </table> <br /> <div id="ErrorMessageBox" style="display: none"> </div> </form> </body> </html>