Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi Friends,
How to set dynamic validations in .Net.

Actually I need to set validation at common place want to use it in my whole Application.

For Example:
i have two pages in my Application.

1. 1st page contain 2 type validation like (Valid Email and only Integer Number Entered)
2. 2nd Page contain only email validation

How can i set these validation at common place and use it in my whole application.
if i change at that common place in validation. it applies on whole application.

I am waiting

Thanks
Posted
Comments
sunandandutt 27-Aug-12 3:19am    
Hi Friends,
Actually I want that if i will make change in validation at one place then it will effect on my complete application wherever i implement that validation.
abhishekdhanotia 27-Aug-12 5:47am    
Best solution for this:
1) Create a user control, contains two elements
a) Email Address (Should be in separeate Div)
b) Numeric Field (Should be in separeate Div)

2) Apply dotnet Required Field Validators on each control.
3) Create two public properties in user control:
a) isEmailVisible
b) isNumericFieldVisible

3) Call this user control whereever you required in your application. In case if you dont required any Element then make public property to false.

4) On Page_Load of User Control, On the basis of public property make field hide / visible, same disable / Enable RequiredFieldValidators.
sunandandutt 27-Aug-12 8:08am    
Good Try Abhishekdhonotia........... I really appriciate your efforts........... Actually my requirement is : i have some properties in my database. and i want that validations should be assigned to perticular property. whenever i call that property it comes with the proper validation which is assigned to it. Thanks.......

Hi Friends,
I found my Solution in this link.
16 steps to write flexible business validation in C# using validation blocks[^]

Thanks for your support..........
 
Share this answer
 
Comments
mahesh1529 28-Aug-12 23:27pm    
Good. My 5!
Hello,

Have a lok at following article in CP. It demonstrates the creation for util class for validation and use it across all the pages:
Common Validation and Length Validation with Regular Expressions in ASP.NET[^]
 
Share this answer
 
Try this code...
 public class Validation
    {

        public bool ValidEmail(string mailId)
        {

            Regex reg = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            if (reg.IsMatch(mailId))
            {
                return true;
            }
            return false;
        }

        public bool ValidNumber(string num)
        {
            Regex regex = new Regex("^[0-9]*$");
            if (regex.IsMatch(num))
            {
                return true;
            }
            return false;
        }
}


if(!Validation.ValidEmail(txtEmail.Text))<br />
{<br />
//Invalid email<br />
<br />
}<br />
<br />
<br />
if(!Validation.ValidNumber(txtnumber.Text))<br />
{<br />
//non numeric value<br />

}
 
Share this answer
 
v3
Comments
sunandandutt 27-Aug-12 8:09am    
Actually my requirement is : i have some properties in my database. and i want that validations should be assigned to perticular property. whenever i call that property it comes with the proper validation which is assigned to it. Thanks.......

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900