In web forms we need to use validation controls at many stages, and in a complete website we have many pages which need validations.
But at every page we have to use those validation controls with full definition. So isn't there a way to use validation controls as we do with css classes, just define once and use anywhere.
Thanks Sohail, but can it work for every kind of validation control I mean for Range, Regular, Required etc...
Can these be used at any page through this script? can you please tell me by defining any one validation control in a class and use that through your script example in one aspx page?
Hi Sohail, I think you did not understand what I asked. In your code sample you are defining validation controls on the aspx page itself. But I want that the validation definition on some other class or anywhere it is suitable and then just call them with
some validation class as we do with css class.
All the validation defination for all validation controls must be defined in single class only from where we can call them like css classes.
for ex, for textbox and ragular expression I want to write code like this
It is hard to do that with normal ASP.NET you can also use HTML Validation in this Asp validator not Require but there is draw of this validation that all browser not support that you have to use some jquery form validation liabrary may be its work for
you
if you are using 4.5 FrameWork then there is Unobtrusive Validation with ASP.NET 4.5 you can try this.
you can also create a javascript file and define all regular expression in this file with differnet function and call this funciton on
onblur event i think its help you
you warmly welcome sophia_ACTIVE SERVICE PAGES if you get any success please also share with me i really appreciate your
ValidationClass concept and may be this concept will be a part of VS in future
sophia_asp
Member
521 Points
469 Posts
validation class like css class
Aug 31, 2012 10:10 AM|LINK
Hello all,
In web forms we need to use validation controls at many stages, and in a complete website we have many pages which need validations.
But at every page we have to use those validation controls with full definition. So isn't there a way to use validation controls as we do with css classes, just define once and use anywhere.
Please tell me if this is possible.
Many thanks
SohailShaikh
Contributor
6109 Points
1167 Posts
Re: validation class like css class
Aug 31, 2012 10:16 AM|LINK
use Asp validtor and call from javascript link this
Sohail Shaikh
sophia_asp
Member
521 Points
469 Posts
Re: validation class like css class
Sep 01, 2012 08:11 AM|LINK
Thanks Sohail, but can it work for every kind of validation control I mean for Range, Regular, Required etc...
Can these be used at any page through this script? can you please tell me by defining any one validation control in a class and use that through your script example in one aspx page?
Many thanks
ramiramilu
All-Star
95275 Points
14072 Posts
Re: validation class like css class
Sep 01, 2012 09:06 AM|LINK
simple way is to define Data Annotations - http://adventuresdotnet.blogspot.in/2009/08/aspnet-webforms-validation-with-data.html
Thanks,
JumpStart
SohailShaikh
Contributor
6109 Points
1167 Posts
Re: validation class like css class
Sep 01, 2012 10:50 AM|LINK
yup its work on every client side asp validator you can also use with validation group just put the name of validatoin in this parametter
Here is some example code
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script> function ShowDiv() { $('#mydiv').show(); } function Validate() { if (Page_ClientValidate()) { $('#mydiv').hide(); } } </script> <div style="background-color:Gray; display:none; position:absolute;" id="mydiv"> Name <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Require" ControlToValidate="TextBox2" ValidationGroup="my" ForeColor="Red"></asp:RequiredFieldValidator> <br /> Age <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Require" ControlToValidate="TextBox3" ValidationGroup="my" ForeColor="Red"></asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Nember only" ValidationExpression="^[0-9]+$" ValidationGroup="my" ControlToValidate="TextBox3" ForeColor="Red"></asp:RegularExpressionValidator> <br /> <asp:Button ID="Button1" runat="server" Text="Hide Div" ValidationGroup="my" OnClientClick="JavaScript:Validate();return false;" /> </div> <asp:Button ID="Button2" runat="server" Text="Show Div" OnClientClick="JavaScript:ShowDiv();return false;" />Sohail Shaikh
sophia_asp
Member
521 Points
469 Posts
Re: validation class like css class
Sep 02, 2012 08:01 AM|LINK
Hi Sohail, I think you did not understand what I asked. In your code sample you are defining validation controls on the aspx page itself. But I want that the validation definition on some other class or anywhere it is suitable and then just call them with some validation class as we do with css class.
All the validation defination for all validation controls must be defined in single class only from where we can call them like css classes.
for ex, for textbox and ragular expression I want to write code like this
Many thanks...SohailShaikh
Contributor
6109 Points
1167 Posts
Re: validation class like css class
Sep 02, 2012 11:13 AM|LINK
It is hard to do that with normal ASP.NET you can also use HTML Validation in this Asp validator not Require but there is draw of this validation that all browser not support that you have to use some jquery form validation liabrary may be its work for you
if you are using 4.5 FrameWork then there is Unobtrusive Validation with ASP.NET 4.5 you can try this.
http://www.asp.net/web-pages/tutorials/working-with-pages/validating-user-input-in-aspnet-web-pages-sites
There is Another Soultion
you can also create a javascript file and define all regular expression in this file with differnet function and call this funciton on onblur event i think its help you
Sohail Shaikh
sophia_asp
Member
521 Points
469 Posts
Re: validation class like css class
Sep 02, 2012 01:36 PM|LINK
Thanks Sohail, I will try the second option. But for the first suggestion isn;t it possible with asp.net 4.0?
SohailShaikh
Contributor
6109 Points
1167 Posts
Re: validation class like css class
Sep 03, 2012 09:07 AM|LINK
you warmly welcome sophia_ACTIVE SERVICE PAGES if you get any success please also share with me i really appreciate your ValidationClass concept and may be this concept will be a part of VS in future
Sohail Shaikh