473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Custom validator

I don't know if it's the right name: I try to explain what I want to do.

Follow me. Wb.config file permits user to create personalized section.
The section can be read throught your ConfigurationSection's derived class
and each
element of the section should belong to a ConfigurationElement's derived
class.

Well. For standard field that return string, integer and so on, for each
attribute
you can use XXXXValidator to check if passed value is in a certain range.
That's fine, but I want to implement my own XXXX Validator.

So what I did is the following.

public sealed class ContentTypeValidator : ConfigurationValidatorBase
{
public override void Validate(object value)
{
ArrayList ct = new ArrayList();

ct.Add("text/plain");
ct.Add("text/HTML");
ct.Add("image/GIF");
ct.Add("image/JPEG");
ct.Add("image/TIFF");
ct.Add("application/pdf");
ct.Add("application/zip");
ct.Add("application/x-excel");
ct.Add("application/rtf");

try
{
if (Array.IndexOf(ct, value) == -1)
throw new ConfigurationErrorsException("Value isn't allowed");

}
catch (ConfigurationErrorsException err)
{
throw err;
}
}
}

I don't know if this is the right way to do what I want, but despite the
class doesn't contain
any formal error, if I got closest to my attribute declaration and press
[ and look for my Validator
class, it isn't there. What's the matter?
Thanks to all
Giuseppe

Jan 14 '06 #1
6 1391
I think that you are overcomplicating things for yourself. There is a
Control called CustomValidator. It has an event called ServerValidate that
is triggered when the control is validated, in which you can place any code
you want. To specify whether it passed validation, do one of the following:

args.IsValid=True

OR

args.IsValid=False

Hopefully this helps, Good Luck!
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Giuseppe Esposito" <giusespo@no_spam_libero.it> wrote in message
news:9f*************************@news.microsoft.co m...
I don't know if it's the right name: I try to explain what I want to do.

Follow me. Wb.config file permits user to create personalized section. The
section can be read throught your ConfigurationSection's derived class and
each
element of the section should belong to a ConfigurationElement's derived
class.

Well. For standard field that return string, integer and so on, for each
attribute
you can use XXXXValidator to check if passed value is in a certain range.
That's fine, but I want to implement my own XXXX Validator.

So what I did is the following.

public sealed class ContentTypeValidator : ConfigurationValidatorBase
{
public override void Validate(object value)
{
ArrayList ct = new ArrayList();

ct.Add("text/plain");
ct.Add("text/HTML");
ct.Add("image/GIF");
ct.Add("image/JPEG");
ct.Add("image/TIFF");
ct.Add("application/pdf");
ct.Add("application/zip");
ct.Add("application/x-excel");
ct.Add("application/rtf");

try
{
if (Array.IndexOf(ct, value) == -1)
throw new ConfigurationErrorsException("Value isn't allowed");

}
catch (ConfigurationErrorsException err)
{
throw err;
}
}
}

I don't know if this is the right way to do what I want, but despite the
class doesn't contain
any formal error, if I got closest to my attribute declaration and press
[ and look for my Validator
class, it isn't there. What's the matter?
Thanks to all
Giuseppe

Jan 15 '06 #2
Well, I need to use it in the web.config file, not in an asp page, so I suppose
that a CustomValidator doesn't suit my needs, does it?

ByeGiuseppe

Jan 16 '06 #3
Well, first of all, the web.config file is an XML file, so you won't be able
to write VB.NET or C#.NET code in it to begin with. The web.config file is
simply a list of settings, so I think I am a little confused about what you
are planning to do with your validation code. If you simply want to write a
class, create a .VB or .CS file (depending on what language you are using).
If you simply want a function or sub that does the validation, you can
either write a class containing nothing except that function or sub, or you
could add the function or sub to the Global class (located in the
Global.asax.vb or Global.asax.cs file). If you could try to clarify what you
are planning on doing with this validator, it might help me and anyone else
figure out what you are looking for. (NOTE: When posting a Reply, it is good
to include the previous message so that people can see anything you have
already tried, mentioned, as well as what the original problem was)
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Giuseppe Esposito" <giusespo@no_spam_libero.it> wrote in message
news:9f*************************@news.microsoft.co m...
Well, I need to use it in the web.config file, not in an asp page, so I
suppose that a CustomValidator doesn't suit my needs, does it?

ByeGiuseppe

Jan 17 '06 #4
Sorry, I just saw that the name and e-mail used for posting is wrongly changed
with another profile.
By the way.

Well, I believe to have said all on the first post, but let me try to explain
it again. Maybe that I wasn't so clear as I mean.

This is an extract of the web.config file

<configSections>
<section name="mySection" type="MyCompany.MySection"/>
</configSections>
<mySection>
<Item name="Giuseppe"/>
</mySection>
Then ... to handle mySection I wrote a class that inherits from ConfigurationSection
in which is defined a metod Item that return
a class that inherits from ConfigurationElement. In this class you must define
each property of Item, otherwise compiler generate an error during the startup.

So here it is the class.
public class Item : ConfigurationElement
{
[ConfigurationProperty("name",
DefaultValue = ",
IsRequired = false)]
public string name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
}

Now you want to be sure that name is in a certain range if it is numeric
or that doesn't contains some invalid characters.
So in case like this you could use the XValidator class; then in case of
the string you probably use the stringValidator, right?

But StringValidator perform a RegEx search, so if you want to match your
value with something that is in a list, this isn't suit your goal.

Your last chance is to implement your own Validator Class.

So this is what I do.

[AttributeUsage(AttributeTargets.Property, AllowMultiple=false)]
public sealed class NameAttribute : ConfigurationValidatorAttribute
{
public ContentTypeAttribute()
{
}

public override ConfigurationValidatorBase ValidatorInstance
{
get
{
return new NameValidator(""); // Here there is a problem
}
}
}

public class NameValidator : ConfigurationValidatorBase
{
String[] ct = new String[] {"Andrea", "Giuseppe"};

public NameValidator (String name)
{
if (base.CanValidate(name.GetType()))
this.Validate(NameValidator);
}

public override void Validate(object value)
{
if (Array.IndexOf(ct, value) == -1)
throw new Exception("Value isn't allowed");
}
}

Basically it should be the same that a XValidator does, but there is one
thing that isn't so clear to me :
how the current value stored in the property Name of the Element Item arrive
to my validator.

Bye
Andrea
NS> Well, first of all, the web.config file is an XML file, so you won't
NS> be able to write VB.NET or C#.NET code in it to begin with. The
NS> web.config file is simply a list of settings, so I think I am a
NS> little confused about what you are planning to do with your
NS> validation code. If you simply want to write a class, create a .VB
NS> or .CS file (depending on what language you are using). If you
NS> simply want a function or sub that does the validation, you can
NS> either write a class containing nothing except that function or sub,
NS> or you could add the function or sub to the Global class (located in
NS> the Global.asax.vb or Global.asax.cs file). If you could try to
NS> clarify what you are planning on doing with this validator, it might
NS> help me and anyone else figure out what you are looking for. (NOTE:
NS> When posting a Reply, it is good to include the previous message so
NS> that people can see anything you have already tried, mentioned, as
NS> well as what the original problem was)
NS>
NS> "Giuseppe Esposito" <giusespo@no_spam_libero.it> wrote in message
NS> news:9f*************************@news.microsoft.co m...
NS>
Well, I need to use it in the web.config file, not in an asp page, so
I suppose that a CustomValidator doesn't suit my needs, does it?

ByeGiuseppe


Jan 18 '06 #5
I think that you're looking for someone who knows a little bit more about
the communication between web.config and the code pages than I do. I have
never dealt with all of the parts of the web.config file you mention, so you
should probably try to find someone who has more experience with them than
me. Sorry I couldn't be of more help.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"andrea" <go**********@freemail.it> wrote in message
news:9f*************************@news.microsoft.co m...
Sorry, I just saw that the name and e-mail used for posting is wrongly
changed with another profile.
By the way.

Well, I believe to have said all on the first post, but let me try to
explain it again. Maybe that I wasn't so clear as I mean.

This is an extract of the web.config file

<configSections>
<section name="mySection" type="MyCompany.MySection"/>
</configSections>
<mySection>
<Item name="Giuseppe"/>
</mySection>
Then ... to handle mySection I wrote a class that inherits from
ConfigurationSection in which is defined a metod Item that return
a class that inherits from ConfigurationElement. In this class you must
define each property of Item, otherwise compiler generate an error during
the startup.

So here it is the class.
public class Item : ConfigurationElement {
[ConfigurationProperty("name",
DefaultValue = ",
IsRequired = false)]
public string name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
}

Now you want to be sure that name is in a certain range if it is numeric
or that doesn't contains some invalid characters.
So in case like this you could use the XValidator class; then in case of
the string you probably use the stringValidator, right?

But StringValidator perform a RegEx search, so if you want to match your
value with something that is in a list, this isn't suit your goal.

Your last chance is to implement your own Validator Class.

So this is what I do.

[AttributeUsage(AttributeTargets.Property, AllowMultiple=false)]
public sealed class NameAttribute : ConfigurationValidatorAttribute
{
public ContentTypeAttribute()
{
}

public override ConfigurationValidatorBase ValidatorInstance { get
{
return new NameValidator(""); // Here there is a problem
}
}
}

public class NameValidator : ConfigurationValidatorBase
{
String[] ct = new String[] {"Andrea", "Giuseppe"};

public NameValidator (String name)
{
if (base.CanValidate(name.GetType()))
this.Validate(NameValidator); }

public override void Validate(object value)
{
if (Array.IndexOf(ct, value) == -1)
throw new Exception("Value isn't allowed");
}
}

Basically it should be the same that a XValidator does, but there is one
thing that isn't so clear to me :
how the current value stored in the property Name of the Element Item
arrive to my validator.

Bye
Andrea
NS> Well, first of all, the web.config file is an XML file, so you won't
NS> be able to write VB.NET or C#.NET code in it to begin with. The
NS> web.config file is simply a list of settings, so I think I am a
NS> little confused about what you are planning to do with your
NS> validation code. If you simply want to write a class, create a .VB
NS> or .CS file (depending on what language you are using). If you
NS> simply want a function or sub that does the validation, you can
NS> either write a class containing nothing except that function or sub,
NS> or you could add the function or sub to the Global class (located in
NS> the Global.asax.vb or Global.asax.cs file). If you could try to
NS> clarify what you are planning on doing with this validator, it might
NS> help me and anyone else figure out what you are looking for. (NOTE:
NS> When posting a Reply, it is good to include the previous message so
NS> that people can see anything you have already tried, mentioned, as
NS> well as what the original problem was)
NS> NS> "Giuseppe Esposito" <giusespo@no_spam_libero.it> wrote in message
NS> news:9f*************************@news.microsoft.co m...
NS>
Well, I need to use it in the web.config file, not in an asp page, so
I suppose that a CustomValidator doesn't suit my needs, does it?

ByeGiuseppe


Jan 18 '06 #6
I found the solution .... I'll post it on my blogs, but it's in italian only.
Maybe I'll translated it in english.

Bye
Andrea

NS> I think that you're looking for someone who knows a little bit more
NS> about the communication between web.config and the code pages than I
NS> do. I have never dealt with all of the parts of the web.config file
NS> you mention, so you should probably try to find someone who has more
NS> experience with them than me. Sorry I couldn't be of more help.
NS>
NS> "andrea" <go**********@freemail.it> wrote in message
NS> news:9f*************************@news.microsoft.co m...
NS>
Sorry, I just saw that the name and e-mail used for posting is
wrongly
changed with another profile.
By the way.
Well, I believe to have said all on the first post, but let me try to
explain it again. Maybe that I wasn't so clear as I mean.

This is an extract of the web.config file

<configSections>
<section name="mySection" type="MyCompany.MySection"/>
</configSections>
<mySection>
<Item name="Giuseppe"/>
</mySection>
Then ... to handle mySection I wrote a class that inherits from
ConfigurationSection in which is defined a metod Item that return
a class that inherits from ConfigurationElement. In this class you
must
define each property of Item, otherwise compiler generate an error
during
the startup.
So here it is the class.

public class Item : ConfigurationElement {
[ConfigurationProperty("name",
DefaultValue = ",
IsRequired = false)]
public string name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
}
Now you want to be sure that name is in a certain range if it is
numeric
or that doesn't contains some invalid characters.
So in case like this you could use the XValidator class; then in case
of
the string you probably use the stringValidator, right?
But StringValidator perform a RegEx search, so if you want to match
your value with something that is in a list, this isn't suit your
goal.

Your last chance is to implement your own Validator Class.

So this is what I do.

[AttributeUsage(AttributeTargets.Property, AllowMultiple=false)]
public sealed class NameAttribute : ConfigurationValidatorAttribute
{
public ContentTypeAttribute()
{
}
public override ConfigurationValidatorBase ValidatorInstance { get
{
return new NameValidator(""); // Here there is a problem
}
}
}
public class NameValidator : ConfigurationValidatorBase
{
String[] ct = new String[] {"Andrea", "Giuseppe"};
public NameValidator (String name)
{
if (base.CanValidate(name.GetType()))
this.Validate(NameValidator); }
public override void Validate(object value)
{
if (Array.IndexOf(ct, value) == -1)
throw new Exception("Value isn't allowed");
}
}
Basically it should be the same that a XValidator does, but there is
one
thing that isn't so clear to me :
how the current value stored in the property Name of the Element Item
arrive to my validator.
Bye
Andrea
NS> Well, first of all, the web.config file is an XML file, so you
won't
NS> be able to write VB.NET or C#.NET code in it to begin with. The
NS> web.config file is simply a list of settings, so I think I am a
NS> little confused about what you are planning to do with your
NS> validation code. If you simply want to write a class, create a
.VB
NS> or .CS file (depending on what language you are using). If you
NS> simply want a function or sub that does the validation, you can
NS> either write a class containing nothing except that function or
sub,
NS> or you could add the function or sub to the Global class (located
in
NS> the Global.asax.vb or Global.asax.cs file). If you could try to
NS> clarify what you are planning on doing with this validator, it
might
NS> help me and anyone else figure out what you are looking for.
(NOTE:
NS> When posting a Reply, it is good to include the previous message
so
NS> that people can see anything you have already tried, mentioned,
as
NS> well as what the original problem was)
NS> NS> "Giuseppe Esposito" <giusespo@no_spam_libero.it> wrote in
message
NS> news:9f*************************@news.microsoft.co m...
NS>
Well, I need to use it in the web.config file, not in an asp page,
so I suppose that a CustomValidator doesn't suit my needs, does it?

ByeGiuseppe


Jan 18 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike P | last post by:
I have a Custom validator that has ControlToValidate set to a textbox that also has a Required and RegularExpression validator. When I run the webpage on my machine the custom validator...
2
by: Evgueni | last post by:
Hello, I am new to .NET, and a custom validator is giving me a lot of grief. I want to use a Custom Validation control and for some reasons it's not firing the validation procedure. I am using...
8
by: pmud | last post by:
Hi, I am using a compare validator in asp.net application(c# code). This Custom validator is used for comparing a value enterd by the user against the primary key in the SQL database. IF the...
5
by: Mattyw | last post by:
Hi, I'm relatively new to Web Forms, I have been using Required Field Validators and Regular Expression Validators on a Web Form I am developing and everything works as expected using Visual...
2
by: Alan Silver | last post by:
Hello, I have a custom validator on my page, and have the server-side code working fine. I want to add a client-side funtion as well, but am not sure how to wire it in so that it works with the...
9
by: wardy1975 | last post by:
Hi All, Looking for a little expert advice on a few web standards issues. I am currently trying to understand the impact of web standards for a web application I work with. I have been doing a...
3
by: Andy | last post by:
Hi folks, I have a customvalidator control that works properly if it isn't contained in an ASP:TABLE. But, when I place it inside an ASP:TABLE, I find that _ServerValidate doesn't get fired,...
0
by: tsw_mik | last post by:
I have created a custom control. It has: -label -button -list of textboxes -list of dropdownlists. I want to use a custom validator to perform some validation, but somehow I can't fo it. The...
4
by: Rick | last post by:
Hello, I built a composite web control that has a textbox and a date control. added my custom control on a webform where there are other standard controls. Each control on the form has a...
1
gagandeepgupta16
by: gagandeepgupta16 | last post by:
Hi I am working on an entry form using validation controls in ASP.NET. I have two controls which requires custom validators, no issue in using plain custom validators. But when i am using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.