RequiredFieldValidator - only for 1 button

D

David Thielen

Hi;

I have 4 buttons in a form. For 1 button, no validation occurs (works fine).
For 2 buttons, every field except 1 text box needs to be validated. For the
4th button, all fields including the text box need to be validated.

How do I get it to not give me the message that the text box is empty, but
still have all other fields validated, when clicking that one button?
 
N

Nathan Sokalski

What I would do in your situation is set all the Buttons' CausesValidation
properties to False, and in their event handlers check the necessary
validators' IsValid properties individually. I am not sure if there is
another way. I have had the same situation in applications of mine. What I
would like to see is an improvement to the Button, LinkButton, and
ImageButton Controls is rather than having a CausesValidation property have
a list of which Validators to validate. The place where I would like to have
this the most is on forms where multiple things are done, such as
administration pages where you can either add a link or upload an image, but
you will never be doing both at the same time, so I have to set
CausesValidation=False for both submit buttons as well as manually check the
validators in my event handlers. Anyway, hopefully my suggestion wil solve
your problem for now. Good Luck!
 
W

Walter Wang [MSFT]

Hi David,

Thank you for your post!

Based on my understanding, the question is: How to enable or disable
individual validators based on which button the user clicks. If I have
misunderstood anything, please feel free to post here.

ASP.NET 2.0 introduces a new "ValidationGroup" property on validation and
input controls. The validation groups help you to group the controls in a
single page and have separate submit buttons for each group, so that you
can submit each group individually. Each group can have separate validation
controls.

To see more detailed information about "ValidationGroup", see following
article written by Scott Guthrie:

http://weblogs.asp.net/scottgu/archive/2004/10/24/246945.aspx

However, your issue still cannot be resolved using this "ValidationGroup"
feature. Since currently "ValidationGroup" property can only specify one
group.

Besides Nathan's suggestion about using server-side events to manually
validate controls, we can use another approach which requires client-side
javascript being enabled.

Here's a sample WebForm which demostrates this technique using your
requirements:

=====================
<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btn1" OnClientClick="javascript:enableValidators();"
runat="server" Text="btn1" />
<br />
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator1" runat="server"
ControlToValidate="txt1" ErrorMessage="required
txt1"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn2"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn2" />
<asp:Button ID="btn3"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn3" />
<br />
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator2" runat="server"
ControlToValidate="txt2" ErrorMessage="required
txt2"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn4"
OnClientClick="javascript:enableValidators(validator1, validator2);"
runat="server" Text="btn4"/>
</form>

<script language="javascript">
enableValidators();

function enableValidators() {
for (i = 0; i < Page_Validators.length; i++) {
ValidatorEnable(Page_Validators, false);
}
for (i = 0; i < arguments.length; i++) {
ValidatorEnable(arguments, true);
}
}
</script>
</body>
</html>
=====================

Hope this helps. If there's anything unclear, please feel free to post here.


Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Thielen

very clever solution - thank you

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com



Walter Wang said:
Hi David,

Thank you for your post!

Based on my understanding, the question is: How to enable or disable
individual validators based on which button the user clicks. If I have
misunderstood anything, please feel free to post here.

ASP.NET 2.0 introduces a new "ValidationGroup" property on validation and
input controls. The validation groups help you to group the controls in a
single page and have separate submit buttons for each group, so that you
can submit each group individually. Each group can have separate validation
controls.

To see more detailed information about "ValidationGroup", see following
article written by Scott Guthrie:

http://weblogs.asp.net/scottgu/archive/2004/10/24/246945.aspx

However, your issue still cannot be resolved using this "ValidationGroup"
feature. Since currently "ValidationGroup" property can only specify one
group.

Besides Nathan's suggestion about using server-side events to manually
validate controls, we can use another approach which requires client-side
javascript being enabled.

Here's a sample WebForm which demostrates this technique using your
requirements:

=====================
<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btn1" OnClientClick="javascript:enableValidators();"
runat="server" Text="btn1" />
<br />
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator1" runat="server"
ControlToValidate="txt1" ErrorMessage="required
txt1"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn2"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn2" />
<asp:Button ID="btn3"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn3" />
<br />
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator2" runat="server"
ControlToValidate="txt2" ErrorMessage="required
txt2"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn4"
OnClientClick="javascript:enableValidators(validator1, validator2);"
runat="server" Text="btn4"/>
</form>

<script language="javascript">
enableValidators();

function enableValidators() {
for (i = 0; i < Page_Validators.length; i++) {
ValidatorEnable(Page_Validators, false);
}
for (i = 0; i < arguments.length; i++) {
ValidatorEnable(arguments, true);
}
}
</script>
</body>
</html>
=====================

Hope this helps. If there's anything unclear, please feel free to post here.


Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

hanuey

hi Walter and david

That you very much it was much simple and fast way , this posting i was
really helpful for me

thanks
regards
kiran


Hi David,

Thank you for your post!

Based on my understanding, the question is: How to enable or disable
individual validators based on which button the user clicks. If I have
misunderstood anything, please feel free to post here.

ASP.NET 2.0 introduces a new "ValidationGroup" property on validation and
input controls. The validation groups help you to group the controls in a
single page and have separate submit buttons for each group, so that you
can submit each group individually. Each group can have separate validation
controls.

To see more detailed information about "ValidationGroup", see following
article written by Scott Guthrie:

http://weblogs.asp.net/scottgu/archive/2004/10/24/246945.aspx

However, your issue still cannot be resolved using this "ValidationGroup"
feature. Since currently "ValidationGroup" property can only specify one
group.

Besides Nathan's suggestion about using server-side events to manually
validate controls, we can use another approach which requires client-side
javascript being enabled.

Here's a sample WebForm which demostrates this technique using your
requirements:

=====================
<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btn1" OnClientClick="javascript:enableValidators();"
runat="server" Text="btn1" />
<br />
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator1" runat="server"
ControlToValidate="txt1" ErrorMessage="required
txt1"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn2"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn2" />
<asp:Button ID="btn3"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn3" />
<br />
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator2" runat="server"
ControlToValidate="txt2" ErrorMessage="required
txt2"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn4"
OnClientClick="javascript:enableValidators(validator1, validator2);"
runat="server" Text="btn4"/>
</form>

<script language="javascript">
enableValidators();

function enableValidators() {
for (i = 0; i < Page_Validators.length; i++) {
ValidatorEnable(Page_Validators, false);
}
for (i = 0; i < arguments.length; i++) {
ValidatorEnable(arguments, true);
}
}
</script>
</body>
</html>
=====================

Hope this helps. If there's anything unclear, please feel free to post here.


Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

hanuey

hi Walter and david

Thank you very much it was much simple and fast way , this posting
was really helpful for me .

thanks
regards
kiran


Hi David,

Thank you for your post!

Based on my understanding, the question is: How to enable or disable
individual validators based on which button the user clicks. If I have
misunderstood anything, please feel free to post here.

ASP.NET 2.0 introduces a new "ValidationGroup" property on validation and
input controls. The validation groups help you to group the controls in a
single page and have separate submit buttons for each group, so that you
can submit each group individually. Each group can have separate validation
controls.

To see more detailed information about "ValidationGroup", see following
article written by Scott Guthrie:

http://weblogs.asp.net/scottgu/archive/2004/10/24/246945.aspx

However, your issue still cannot be resolved using this "ValidationGroup"
feature. Since currently "ValidationGroup" property can only specify one
group.

Besides Nathan's suggestion about using server-side events to manually
validate controls, we can use another approach which requires client-side
javascript being enabled.

Here's a sample WebForm which demostrates this technique using your
requirements:

=====================
<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btn1" OnClientClick="javascript:enableValidators();"
runat="server" Text="btn1" />
<br />
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator1" runat="server"
ControlToValidate="txt1" ErrorMessage="required
txt1"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn2"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn2" />
<asp:Button ID="btn3"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn3" />
<br />
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator2" runat="server"
ControlToValidate="txt2" ErrorMessage="required
txt2"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn4"
OnClientClick="javascript:enableValidators(validator1, validator2);"
runat="server" Text="btn4"/>
</form>

<script language="javascript">
enableValidators();

function enableValidators() {
for (i = 0; i < Page_Validators.length; i++) {
ValidatorEnable(Page_Validators, false);
}
for (i = 0; i < arguments.length; i++) {
ValidatorEnable(arguments, true);
}
}
</script>
</body>
</html>
=====================

Hope this helps. If there's anything unclear, please feel free to post here.


Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

ashu4u2smile

hi,

i have a page which has 4 textbox controls and a save button control.

the textboxes are :

txtName
txtAge
txtContractNbr
txtContractExpire

i have to check for two conditions.

1.) The Name and age textboxes should have a value entered i.e.
required.

2.) Either both the txtContractNbr and txtContractExpire textboxes
should have value entered, or both should be empty.


i tried using the required field validators for the first two textboxes
and in the onClientClick event of the save button i called a javascript
that checks for the second condition. This only checks for the second
condition.

whereas, if i add the same function using the
RegisterClientScriptBlock, to the onclick event of the button, it
checks for both condition, but if the first is validated, it postsback.

i dont know how to couple these two validations. How do i get this
done?

thanks,
Ashu.


Hi David,

Thank you for your post!

Based on my understanding, the question is: How to enable or disable
individual validators based on which button the user clicks. If I have
misunderstood anything, please feel free to post here.

ASP.NET 2.0 introduces a new "ValidationGroup" property on validation and
input controls. The validation groups help you to group the controls in a
single page and have separate submit buttons for each group, so that you
can submit each group individually. Each group can have separate validation
controls.

To see more detailed information about "ValidationGroup", see following
article written by Scott Guthrie:

http://weblogs.asp.net/scottgu/archive/2004/10/24/246945.aspx

However, your issue still cannot be resolved using this "ValidationGroup"
feature. Since currently "ValidationGroup" property can only specify one
group.

Besides Nathan's suggestion about using server-side events to manually
validate controls, we can use another approach which requires client-side
javascript being enabled.

Here's a sample WebForm which demostrates this technique using your
requirements:

=====================
<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btn1" OnClientClick="javascript:enableValidators();"
runat="server" Text="btn1" />
<br />
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator1" runat="server"
ControlToValidate="txt1" ErrorMessage="required
txt1"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn2"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn2" />
<asp:Button ID="btn3"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn3" />
<br />
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator2" runat="server"
ControlToValidate="txt2" ErrorMessage="required
txt2"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn4"
OnClientClick="javascript:enableValidators(validator1, validator2);"
runat="server" Text="btn4"/>
</form>

<script language="javascript">
enableValidators();

function enableValidators() {
for (i = 0; i < Page_Validators.length; i++) {
ValidatorEnable(Page_Validators, false);
}
for (i = 0; i < arguments.length; i++) {
ValidatorEnable(arguments, true);
}
}
</script>
</body>
</html>
=====================

Hope this helps. If there's anything unclear, please feel free to post here.


Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
N

Nathan Sokalski

Well, write a function that calls the other functions, and have the client
events call this function. In other words, put all the validation functions
inside another function so that they will always get called. Also, for your
second condition, are you using a CustomValidator with a JavaScript function
assigned to the ClientValidationFunction, or are you just making a
JavaScript function? If you use the Validator Controls for any of your
validation, you should use them for all of your validation. This could be
the reason for your problem. Good Luck!
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

hi,

i have a page which has 4 textbox controls and a save button control.

the textboxes are :

txtName
txtAge
txtContractNbr
txtContractExpire

i have to check for two conditions.

1.) The Name and age textboxes should have a value entered i.e.
required.

2.) Either both the txtContractNbr and txtContractExpire textboxes
should have value entered, or both should be empty.


i tried using the required field validators for the first two textboxes
and in the onClientClick event of the save button i called a javascript
that checks for the second condition. This only checks for the second
condition.

whereas, if i add the same function using the
RegisterClientScriptBlock, to the onclick event of the button, it
checks for both condition, but if the first is validated, it postsback.

i dont know how to couple these two validations. How do i get this
done?

thanks,
Ashu.


Hi David,

Thank you for your post!

Based on my understanding, the question is: How to enable or disable
individual validators based on which button the user clicks. If I have
misunderstood anything, please feel free to post here.

ASP.NET 2.0 introduces a new "ValidationGroup" property on validation and
input controls. The validation groups help you to group the controls in a
single page and have separate submit buttons for each group, so that you
can submit each group individually. Each group can have separate
validation
controls.

To see more detailed information about "ValidationGroup", see following
article written by Scott Guthrie:

http://weblogs.asp.net/scottgu/archive/2004/10/24/246945.aspx

However, your issue still cannot be resolved using this "ValidationGroup"
feature. Since currently "ValidationGroup" property can only specify one
group.

Besides Nathan's suggestion about using server-side events to manually
validate controls, we can use another approach which requires client-side
javascript being enabled.

Here's a sample WebForm which demostrates this technique using your
requirements:

=====================
<%@ Page Language="C#" %>
<!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 id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="btn1" OnClientClick="javascript:enableValidators();"
runat="server" Text="btn1" />
<br />
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator1" runat="server"
ControlToValidate="txt1" ErrorMessage="required
txt1"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn2"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn2" />
<asp:Button ID="btn3"
OnClientClick="javascript:enableValidators(validator1);" runat="server"
Text="btn3" />
<br />
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="validator2" runat="server"
ControlToValidate="txt2" ErrorMessage="required
txt2"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="btn4"
OnClientClick="javascript:enableValidators(validator1, validator2);"
runat="server" Text="btn4"/>
</form>

<script language="javascript">
enableValidators();

function enableValidators() {
for (i = 0; i < Page_Validators.length; i++) {
ValidatorEnable(Page_Validators, false);
}
for (i = 0; i < arguments.length; i++) {
ValidatorEnable(arguments, true);
}
}
</script>
</body>
</html>
=====================

Hope this helps. If there's anything unclear, please feel free to post
here.


Regards,
Walter Wang
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,122
Messages
2,570,716
Members
47,283
Latest member
VonnieEwan

Latest Threads

Top