My Profile

Showing posts with label ASP.Net Validation Controls. Show all posts
Showing posts with label ASP.Net Validation Controls. Show all posts

Thursday, February 10, 2011

ASP.Net - CustomValidator



1.    CustomValidator:
·       If all above four validation controls doesn’t fulfill our requirement then we have to use Customvalidator and write our own logic.
·       Again for client side validations, we have to write javascript code and for server side validations, we have to write C# code.

How to use CustomValidator for Client side validations?

·       Place Customvalidator and set the following properties:
 ControlToValidate       :
 ErrorMessage     :
 ClientValidationFunction*   : <JS func name>

·       Goto source view and implement JS function inside <script> tag.

This function should be specified with two arguments just like any .net event procedure arguments.
·        
Write code in this function to perform validation and return Yes/No to CustomValidator.
Example JS function:
<script type=”text/javascript”>
     function CheckQty(x , y)
     {
              If(y.Value % 5 == 0)
                        y.IsValid = true;
              else
                        y.IsValid = false;
     }
</script>

How to use CustomValidator for Server side Validations?

Server side validations means, when we submit the form it will be executed at server side. If any other code is there then as usual server will run that code also.

To find whether validation in the current page is successfully validated, we can use “Page.IsValid” property. This property returns true, if all validation controls in the page are successful. Otherwise, it returns false.
Follow the following steps to check this…

·       Place CustomValidator in the form and set the regular properties like:
 ControlToValidate :
 Errormessage  :
And other common properties…
·       Goto Properties window and in Events tab, select “ServerValidate” event. Double click on it; it will display a function in code window, where we have to write the validation code. Again using two arguments which are provided by even procedure or function.

While working with CustomValidator for server side validations and client side validations we have to be clear in points, those are:

·       How it is executed? **
·       For any reason submit doesn’t happen then server side validation will not execute.
·       When we submit the form to server, validation code will run but also other code (if any) will also be executed.

Let’s see an example to make it clear.

Example:
Design the form as following


·       Place a CustomValidator control beside textbox and set the following Properties
  ControlToValidate   :   TextBox1
  ErrorMessage            :  Quantity must be Below the Qty    Available.
  ForeColor                  :  Red

·       Goto Events of that CustomValidator and double click on “ServerValidate” event and write the following code in the function displayed.
if (Convert.ToInt32(args.Value) > 500)
            args.IsValid = false;
         else
            args.IsValid = true;

·        Under Button Click event:

if (Page.IsValid)
            Response.Write("Order Placed.....");
          else
            Response.Write("Please correct Errors!");




Yes! We are done with all Validation controls. Hope you all can do any validation in web forms now.

 
---------------------------------------------------------- 
** If you like this post please like our page in 
facebook " Dot Net Support"


ASP.Net - RegularExpressionValidator


 
1.    RegualrExpressionValidation Control:
·       With this control, we can perform validations by providing an expression, which is nothing but some characters indicating one format or a validation.
·       Many validations like checking for length of entered value, accepting only integers, alphabets or combination can be performed using this validator.
·       It is a language feature to support expression based validation and also some calculations.
·       .Net provides “System.Text.RegularExpressions” namespace which contains classes to perform expression based validations.
·       In ASP.Net RegularExpressionValidator also provides the same functionality. The advantage of using these expressions is to get more accuracy in validation as well as without writing code.

Some characters in regular expressions:
^        Beginning
$       Ending
{}       Length
\d      Digits
\w     word
[]       Range
And many more…

Examples:
  • [A-Za-z0-9]{6} : 6 alphabets (caps and small)/numbers/combination
  • [A-Za-z]{4}\d{2}  : first 4 characters alphabets and next 2 characters digits.
  • \d{10,12}  : digits, minimum 10 and maximum 12.


---------------------------------------------------------- 
** If you like this post please like our page in 
facebook " Dot Net Support"

ASP.Net - CompareValidator & ValidationSummary


Now we’ll see few more validation controls…

1.    CompareValidator:
·       It is a control which performs all validations related to comparison of controls.
·       This control can be used to perform 3 types of comparisons. Those are:
i.                   Control – Control:
With this use of this CompareValidator control, we can compare two different controls.
Apart from common properties this control has some more properties, those are:
Properties:
·       ControlToValidate : <controlname>
·       ControlToCompare : <controlname to which we are comparing>
·       Type : data type
·       Operator : Equal/NotEqual/GreaterThan….
ii.                Control – Value:
With this use of this control, we can compare the control with some value we specify.
Properties:
·       ControlToValidate :
·       ValueToCompare :
·       Type :
·       Operator :

iii.              Control – Data Type:
With this use of this property, we can compare the control with Data Type specified.

Properties :
·       ControlToValidate :
·       Type :
·       Operator : DataTypeCheck *

2.    ValidationSummary:
·       This control doesn’t perform any validations but reports all validation control error messages at one place. That is why it is called summary control. Display property with None value can be used for the Validation control Error messages in this kind of situations. Because, we want to display errors only at one location and that is near control or separately.
·       To use it, we have to just place it on the form and it automatically gets messages.
Properties:
·       ShowMessageBox : true/false(d)
When true, then VS will display error messages in separate alert window.
·       ShowSummary: true(d)/false
When false, within the form, summary control will not display the errors.

Note: when SummaryControl is present in form then ErrorMessage will appear in Summary control and Text will appear in place of validation Control (Useful).

 
---------------------------------------------------------- 
** If you like this post please like our page in 
facebook " Dot Net Support"

ASP.Net-RequiredFieldValidator & RangeValidator


Let’s see one by one control in detail:

1.    RequiredfieldValidator:
·       Used to check for null values and it is the only control which can check null values.
·       Properties:
ControlToValidate   :   <ControlName>

ErrorMessage          :    Message to be displayed when        error occurs.

 Text                          :     Same like error message but differs when   ValidationSummary Control is used.

 SetFocusOnError     :     true/false  [Automatically sets the cursor to failed validation control]

Note: Above properties are used for RequiredfieldValidator and also they are common properties for every Validation Control.

Example: 

Place a TextBox or any control and also place RequiredFieldValidator control beside it and set the above properties and test how it works.

2.    RangeValidator:
·       Used to check for range of values. It is suitable for numeric and date type of data.
·       Properties:
 MinimumValue       :        <some value> [inclusive of this value]
 MaximumValue       :        ------------
 Type                         :        Integer/Date/……

Note: we cannot use validations for all the controls. For example, we cannot use any validation for “CheckBox” because it has only 2 options checked or unchecked.

Before going to see remaining validation control, we have to see one most important property “Display”, which is also a common property to all the validation controls.

Display:
It is an enumerated property which has the following values:
·       Static
·       Dynamic
·       None

This property is related to position of validation control or Message that is to be displayed. By default, it is Static, which means the place to display Error Message will be reserved. Dynamic means Error Message place will not be reserved and only when error occurs it will create the place and display error message. None means no display of error message.
In the next post we’ll see few more validation controls…

 
---------------------------------------------------------- 
** If you like this post please like our page in 
facebook " Dot Net Support"

ASP.Net Validation Controls Introduction


Hello Friendzz…

Till now we have worked with different controls, navigation between forms, and how to work (data binding) in the web environment etc., 

and now we are going to discuss about a new topic “Validations”.

It is also a major requirement in the web forms. Every web page requires validations in client side as well as server side. Validation means “simple set of statements that are written to check whether the given input is in correct format or with correct values”.

Client side validations means the validations which are to be performed on client side i.e., say Browser.

Ex: checking whether an input (say User Id) is provided or not is client side validation.

Server side validations mean the validations which are to be performed on server side.

Ex: checking whether the entered user id already exists in Database or not is server side validation.

How every web developer performs these validations?
·        
     For client side validations, Java Script coding is written and sent to client for execution.
·       For server side, validations are performed using server side programming language.

But in both cases user must write code to perform validations.

How to perform validations in ASP.Net web forms?

As part of its controls, ASP.Net provides one category called “Validation”. In this category, we have validation controls which automate the process of performing validation. Every ASP.Net validation control generates Javascript code for client side validations and server side code for server validations automatically.

User has to provide few inputs to mention what type of validation is required.

ASP.Net supports the following Validation Controls to perform different requirements:
·       RequiredFieldValidator
·       RangeValidator
·       CompareValidator
·       RegularExpressionValidator
·       CustomValidator
·       ValidationSummary
·       Dynamicvalidator (.Net 4.0)

 

---------------------------------------------------------- 
** If you like this post please like our page in 
facebook " Dot Net Support"

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More