|
|
Comments and Discussions
|
|
 |

|
Requires the following assembly configuration for .NET 4.0:
[assembly: System.Security.AllowPartiallyTrustedCallers]
[assembly: System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]
|
|
|
|

|
Adam, this is an excellent article!
Inspired by your validation control, I've published the article Rule Based Validation for ASP.NET[^].
PS. Got my 5!
Cheers,
Jani Giannoudis
Meerazo.com - Resource Sharing Made Easy | Co-founder
|
|
|
|
|

|
I have this case where a field is required unless a check box is check. There are two addresses. If the user check the check box, the assumption is that the two addresses are the same. If not the address fields for the second address are required.
This doesn't work with your control, it crashes. Any thoughts?
Bob Avallone
Bob Avallone
MetaPro Systems Inc.
www.metaprosystems.com
|
|
|
|

|
Hi there,
Is there a way to include the ValidateEmptyText property that you have with a CustomValidator?
I have a whole lists that I either want filled in or leave them all blank to skip.
Thanks!
|
|
|
|

|
Great job! Saved me a huge headache! Thanks!
|
|
|
|
|

|
This is a great control and works perfectly thank you very much, Adam.
But the message stays until submit button is pressed. This may confuse some users.
So based on examples provided by others I've modified your validator so it vould validate on key press in textboxes.
If anybody interested here is how you do that:
Download source of this project. In file "MultipleFieldsValidator.cs" add the following function:
private string getClientStartupScript()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("");
sb.AppendLine("var mpvlc = document.getElementById(\"" + ClientID + "\");");
sb.AppendLine("function alertit() { ValidatorValidate(document.getElementById(\"" + ClientID + "\")); }");
foreach (string cid in GetControlsToValidateIDs())
{
var fid = this.Parent.FindControl(cid);
if (fid != null)
{
sb.AppendLine("var " + cid + "_tid = document.getElementById(\"" + fid.ClientID + "\");");
sb.AppendLine(cid + "_tid.onkeypress = alertit;");
}
}
//return "";
return sb.ToString();
}
Than, in
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
function add the following line:
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "MultipleFieldsControlsValidator" + this.UniqueID, this.getClientStartupScript(), true);
You can also sign up for another events. Just change sb.AppendLine(cid + "_tid.onkeypress = alertit;"); or add multiple linse for multiple events.
|
|
|
|

|
I'm triying the control with two radcombobox (Telerik) withouth success. Any chance that you could help me?:
|
|
|
|

|
Hi, i would like to have this code in vb code. I tried to convert it by myself but no luck it does'nt render correctly. Appreciate if i can have the control in vb code. TQVM
|
|
|
|

|
thanx really helpful.....
|
|
|
|

|
I am glad it helped Make it simple, as simple as possible, but not simpler.
|
|
|
|

|
Hi Adam
I am facing the below problem,can you please help me
I installed the component and using for Email and phone validation (User need to be prompted to fill either one) .In the same page i have Drop down list and based on the user selection it shows few controls on the page, for that i wrote server side code. Now the problem is if i press the submit button with out choosing any thing in drop down list then it prompts that user need to fill either email or phone number.
If the user selects any item in drop down list (this perform server side program) and if i press submit button then the component is not checking for validation of email or phone(both are empty)
By the way I am Using AJAXforASPNET as well
I need help in this
Regards
Sree(Sreedhar)Sree(Sreedhar)
|
|
|
|

|
Thank you! Helped me very much!
|
|
|
|

|
Adam, Just to say thanks for this - Solved a problem for me quickly and elegantly.
|
|
|
|

|
I am glad it helped you.
Make it simple, as simple as possible, but not simpler.
|
|
|
|

|
This validator saved me a ton of time. Thanks very much.
|
|
|
|

|
Very good - this is definitely something missing from the ASP.NET framework.
Just one point; the validation is not updated when the field loses focus. This is due to the JavaScript property 'controltovalidate' not being set. A solution would be to call 'ValidatorHookupControl' for each control in the 'controlstovalidate' property.
Colin Breame
|
|
|
|

|
Hi John,
Thank you for the note. This 'controltovalidate' wasn't set on purpose as this is related to more than one field and the reason is that it wouldn't be a good user experience to report an error while the user is still filling the fields. However, you can argue that as some websites already do that.
Regards,
Adam
Make it simple, as simple as possible, but not simpler.
|
|
|
|

|
Firstly Adam - fabulous validator!
I kept on getting an object undefined error on MultipleFieldsValidatorEvaluateIsValid on 3.5, but tweaked the OnPreRender to look like the following and it works just greate:
If Not Me.Page.ClientScript.IsClientScriptIncludeRegistered(Me.GetType(), "CustomValidators") Then
Me.Page.ClientScript.RegisterClientScriptInclude(Me.GetType(), "CustomValidators", Me.Page.ClientScript.GetWebResourceUrl(Me.GetType(), "CustomValidators.MultipleFieldUIValidators.js"))
End If
|
|
|
|

|
Thank you Jaquie,
I am currently using it with ASP.NET 3.5 but didn't get any error, however, thank you for the note and I am glad that you liked it
Regards,
Adam
Make it simple, as simple as possible, but not simpler.
|
|
|
|

|
Any chance of adding an InitialValue property, useful for validating DropDownLists? I cannot simply have a null string as my default value, so being able to set it to 0, for example, would be a big help.
|
|
|
|

|
hi,
To trigger validation for a DropDownList item, set the item's value to one empty space " ".
Adam
|
|
|
|

|
This is not feasible for me, since the initial value is also a default value on a databound field, which is an integer.
|
|
|
|

|
I added this to the control so that when one of the fields looses focus it will rerun the validation and remove the error message.
Add this to to AddAttributesToRender
Page.ClientScript.RegisterStartupScript(Me.GetType, ("MultipleFieldsControlsValidator" + Me.UniqueID), Me.getClientStartupScript, True)
Add this function
Private Function getClientStartupScript() As String
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder
For Each s As String In Me.GetControlsToValidateIDs
sb.Append("ValidatorHookupControl(document.getElementById('")
sb.Append(s)
sb.Append("'),document.getElementById('")
sb.Append(Me.ClientID)
sb.Append("'));" & vbCrLf)
Next
Return sb.ToString
End Function
C#
this.RegisterStartupScript(this.GetType(), "MultipleFieldsControlsValidator" + this.UniqueID, this.getClientStartupScript(), true);
private string getClientScript()
{
string script = "function BooleanControlsEvaluateIsValid(val) {\r\n" +
"var validatorValue = false;\r\n" +
"var returnValue = false;\r\n" +
"controltovalidateIDs = val.controlstovalidate.split(',');\r\n" +
" for (var controltovalidateIDIndex in controltovalidateIDs) {\r\n" +
" var control = document.getElementById(controltovalidateIDs[controltovalidateIDIndex]);\r\n" +
" if (control.checked) {\r\n" +
" returnValue = true;\r\n" +
" break;\r\n" +
" }\r\n" +
" }\r\n" +
" return returnValue;\r\n" +
"}\r\n;";
return script;
}
Enjoy
|
|
|
|

|
Hi jjhale811,
Thank you for adding this.
I didn't test this code or validated it, however, by looking at the code, here are my comments:
1 - Shouldn't the JS go in the included JS file rather than being emitted in the code? This will serve better perforamce and cleaner code.
2 - What is the purpose of using this: "MultipleFieldsControlsValidator" + this.UniqueID ?
Kind regards,
Adam Tibi
|
|
|
|

|
This is a very good solution. Keep it up!
|
|
|
|
|
|

|
Thank you, I am glad you liked it.
|
|
|
|

|
I am currently using this control in an update panel. When I submit the form the validation works great. But when their is a partial page postback which occurs when the user selects a different option in a drop down box, the validation control's properties are lost. I witnesses this using Firebug in Firefox. After the postback from the drop down control, the Page_Validators recognizes it as a validator control, but the condition and controlstovalidate are missing. Has anyone gotten these results or figured out a way around this?
|
|
|
|

|
Im using this control inside an updatepanel and it does not stop the postback from occuring when the validation fails....it still allows the update panel to post back...
good work on the control
Justin
|
|
|
|

|
Justin,
According to the MS AJAX documentation[^], client script enabled validators are not supported inside update panels with partial-rendering with the standalone library. You can still get the server-side validation, but it seems the client-side validation conflicts with the update panel's agenda.
With the System.Web.Extensions library that is included with .NET 3.5, validators are supported. However, you must register your scripts via the ScriptManager instead of the ClientScriptManager. Here is a snippet from the BaseValidator with regards to registering expando attributes.
...
Page page = control.Page;
if (!page.IsPartialRenderingSupported) {
page.ClientScript.RegisterExpandoAttribute(controlId, attributeName, attributeValue, encode);
} else {
control.Page.ScriptManagerType.InvokeMember(
"RegisterExpandoAttribute",
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
null,
null,
new object[] { control, controlId, attributeName, attributeValue, encode }
);
}
...
Unfortunately, you cannot get at the Page.IsPartialRenderingSupported or Page.ScriptManagerType properties because they are internal. However, you can use the ScriptManager's static GetCurrent method to get at the current script manager on the page, if any.
I recommend creating new methods in the validator class for RegisterExpandoAttribute and RegisterClientScriptResource. These new methods will check for the need to register via the Page.ClientScript instance or ScriptManager dynamically by calling another method; say, IsPartialRenderingEnabled. It would look something like this:
private Type _scriptManagerType = null;
private bool? _isPartialRenderingSupported = null;
protected Type ScriptManagerType {
get {
if (_scriptManagerType == null) {
_scriptManagerType = BuildManager.GetType("System.Web.UI.ScriptManager", false);
}
return _scriptManagerType;
}
}
public bool IsPartialRenderingSupported {
get {
if (!_isPartialRenderingSupported.HasValue) {
_isPartialRenderingSupported = false;
if (ScriptManagerType != null) {
object CurrentScriptManager = ScriptManagerType.InvokeMember(
"GetCurrent",
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
null,
null,
new object[] { this.Page }
);
if (CurrentScriptManager != null) {
PropertyInfo Property = scriptManagerType.GetProperty("SupportsPartialRendering");
if (Property != null) {
object PropertyValue = Property.GetValue(CurrentScriptManager, null);
_isPartialRenderingSupported = (bool)PropertyValue;
}
}
}
}
return _isPartialRenderingSupported.Value;
}
}
public void RegisterExpandoAttribute(string controlId, string attributeName, string attributeValue) {
if (!IsPartialRenderingSupported) {
this.Page.ClientScript.RegisterExpandoAttribute(controlId, attributeName, attributeValue);
} else {
ScriptManagerType.InvokeMember(
"RegisterExpandoAttribute",
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
null,
null,
new object[] { this, controlId, attributeName, attributeValue, false }
);
}
}
public void RegisterClientScriptResource(Type type, string resourceName) {
if (!IsPartialRenderingSupported) {
this.Page.ClientScript.RegisterClientScriptResource(type, resourceName);
} else {
ScriptManagerType.InvokeMember(
"RegisterClientScriptResource",
BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
null,
null,
new object[] { this, type, resourceName }
);
}
}
Normally, you would just replace your calls to Page.ClientScript.xxx with calls to the ScriptManager.xxx equivalents. However, if you want this to work in .NET Frameworks prior to 3.5, you need to check that ScriptManager even exists and invoke it via reflection. Therefore, your calls need to use the new methods defined for the validator above.
I never said it would be easy or pretty. Good luck!
|
|
|
|

|
Matt,
this worked a treat for me .
Adam, if you're still keeping an eye on this it could be a worthy update to your control!
|
|
|
|

|
Thank you Matt,
However, today I am an ASP.NET MVC developer, so validators are now old school
Make it simple, as simple as possible, but not simpler.
|
|
|
|

|
I'm right there with you, Adam!
|
|
|
|

|
Just what I needed, and beautifully easy to use.
|
|
|
|

|
Thank you, I am glad I helped.
|
|
|
|

|
Except that now it's gone wrong I expect I've done something silly, this is my first ASP project, first time using VS, first time using VB.
The page where I'm using the validator works, perfectly, just as I'd wanted. I installed it, it worked, and no, I don't now remember what I did to achieve this other than "followed the instructions".
But I'm now trying to use the debugger in VS (yes, I said I was new to this, didn't I?) It insists on rebuilding the entire site, not just the page I'm trying to debug, so that includes the page with your validator on it. And it says "Unknown server tag 'atv:MultipleFieldsValidator'."
Which is completely silly: the thing IS known, and in use. It works. Any suggestions on what I've done wrong? VS 2005, ASP.Net 2.0
Looking around, there's a bit at the top of the page that says
<%@ Register Assembly="AdamTibi.Web.UI.Validators" Namespace="AdamTibi.Web.UI.Validators"
TagPrefix="atv" %>
I don't even know what else to look for, or where to look for it.
|
|
|
|

|
Did you reference the assembly in your VS Project?
Also if it doesn't know the tag but doesn't issue an error, this sometimes happen in visual studio and is not a show stopper.
|
|
|
|

|
It's OK, this seems to have been a problem with the debugger, not with your code. For some reason, the silly thing decided to look for it on my local machine, not on the webserver that holds the site, your code, and anything else even vaguely relevant.
|
|
|
|
|

|
Hi Jay,
A quick solution could be is adding the validator on the page's HTML rather than using the designer. Have a look at the sample project included and use the same syntax. Removing the JavaScript is not the problem.
Best regards,
Adam Tibi
|
|
|
|

|
Worked like a charm in my VS2008 .Net 3.5 with AJAX web project!
Stevishere
www.Em8s.net
|
|
|
|

|
Thank you mate, I am glad this helped.
I am using it in AJAX applications as well and it is working perfectly.
Regards
Adam Tibi
|
|
|
|

|
Thanks a lot Adam for this handy control..
I came across to this control when i was about something similar myself.
I tested it before using in my project. It is working fine with IE7, Ajax, Validation Groups.
Great job Man !!!!!!!!!
|
|
|
|

|
Hi Mate,
You are welcome, it is always good to know that I helped. Thank you for confirming Ajax, Validation Groups as I found some posts claiming that it doesn't work with AJAX, however, I have used it with Ajax without any problem but I didn't have the chance to do a deeper level of testing.
Best regards
Adam Tibi
|
|
|
|

|
I want to validate the checkbox along with Dropdownlist.But its giving error.Validation is "If checkbox is checkbox is checked then Dropdownlist should have a value.
Can you pls suggest any solution?
|
|
|
|

|
Hi,
The erros message that you have mentioned doesn't even exist in the Multiple Fields Validator, are you sure you are posting to the correct article?
Please also note that you CANNOT validate a checkbox, check my article:
Check Validator - An ASP.NET Validator Control for explanation and for a validator that can validate a checkbox.
Regards
Adam
|
|
|
|

|
I am posting at correct article.Thanks a lot.Both the code have helped me a lot and I have combined the above two to fit my req.
Thanks again adam.
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
|
Discussing the MultipleFieldsValidator that validates a group of fields in which at least one is required, like phone number, mobile phone number, or email. It inherits the BaseValidator and uses some new cool ASP.NET 2.0 features.
| Type | Article |
| Licence | Ms-PL |
| First Posted | 20 Mar 2006 |
| Views | 341,793 |
| Bookmarked | 131 times |
|
|