Showing posts with label Validation. Show all posts
Showing posts with label Validation. Show all posts

Friday, 24 January 2014

Click Here to Download ImplementIValidateObjectInterfaceInMVC.zip Example.

We can validate model itself by implementing IValidatableObject Interface. For validation you need to implement 'Validate' method of IValidatableObject Interface.

Here is some difference from the attribute validation version.
  • In this for validation method is 'Validate' instead of 'IsValid' and also return type and parameter is different.
  • The return type for Validate is an IEnumerable<ValidationResult>, because the logic inside this method is validating the entire model and might need to return more than a single validation error.
  • No parameter is passed to Validate because you  can directly access model properties because this Validate method is inside model.

Here is example for this.

Wednesday, 1 January 2014

Click Here to Download CustomAnnotationInMVC.zip Example.

You can write your own custom annotations in MVC 4.
Here we can do custom validation using custom annotations. Custom annotations validation derive from the ValidationAttribute base class. This base class available in 'System.ComponentModel.DataAnnotations' namespace. ValidationAttribute class is abstract class.

For custom validation you need to write your own class and inherit ValidationAttribute class. After that, you need to override IsValid Method. IsValid Method has two parameters 'Value' and 'ValidationContext'.

  • Value : Which contains your data, which is going to validate.
  • ValidationContext : This parameter is very important. This content information like model object instance, model type friendly display name and other information
In this method you write your own logic for validate data and return result. If value is validate at that time you need to return this 'ValidationResult.Success' else you return result like 'return new ValidationResult(your custom error message)'

Sunday, 18 November 2012

There is a situation in drop down list where first item is "[Select]" and you need to make mandatory selection from drop down list except "[Select]".
Means, user must select value from drop down list but did not allow to select first element "[Select]". We can achieve this using required field validator control. For that we need to set "InitialValue" property.

Here is example for this.
In this example we take one product drop down list control in this list we list out all products, But the First Item is "[Select]" and it's value is "-1" in product drop downlist.
And We take one Required Field Validator control and set it's ValidateControl property to product drop down list id. We also set "InitialValue=-1" property.
We also took one button so that on click of button required field valdator control validate user input.
In this example we set InitialValue to -1 so that , if user select "[Select]" item from drop down list and click on "Save" button Validator control executed and validation message display on screen.
You can see that validation message in output.

Monday, 8 October 2012

We often get requirement to allow only numeric decimal value in textbox for validation purpose, And also we have to specify number of decimal places. We can use ASP.Net RegularExpressionValidator control for this. In this control we can specify regex expression for validation.

The regex expression is "^[+]?[0-9]\d{0,5}(\.\d{1,4})?%?$" .
This regex allow only positive numeric value of maximum six digit and maximum four digit decimal value. The maximum allowed value by this regex is "999999.9999". You can change this accroding to your requirement.

Here is example.

Thursday, 4 October 2012

For validation purpose in your project at some places you put text boxes that accept only integer value i.e. 1, 20 , 50, 200 etc... To put validation on that text boxes and allow only integer value you can use ASP.NET Regular Expression Validator Control. In this control we make one regex expression and do validation for that text box.
The regex expression is "^\d+$" .

Here is example.

Thursday, 23 August 2012

 Click Here to Download TextBoxMaximumLengthApplication.zip

There is always a need of application to set maximum length allowed in textbox.
In this article we demonstrate both single line textbox and multi line textbox max length validation. 
There is a different way to set maximum characters allowed for single line textbox and multiline textbox.
You can set "MaxLength" property for single line textbox.

 For multiline textbox "MaxLength" does not supported.

So for Multi Line textbox we need some tricky approch. We can use javascript , JQuery or server side checking. Here we are going to use .Net Validator controls. We are using "RegularExpressionValidator" control.

Here is example for this.

In this example we are taking two textbox one is for ProdutTitle which is singleline textbox and other is for ProductDescription which is MultiLine textbox. For ProductTitle we are using MaxLength property to set maximum allowed characters. Which is 20 characters.
For ProductDescription we are using "RegularExpressionValidator" control to set maximum allowed characters. Which is 50 characters.
We are using Regex expression for validation.
The "RegularExpressionValidator" control display error message after typing in textbox focus or cursor moved out from textbox.
You can also make server side validation check for regular expression validation using "Page.Validate()" Method and "Page.IsValid" property.

In output you can see that if more than maximum allowed characters type in textbox it will give error message. You can also make you customize message.


ASPX Code:
        <table>
            <tr>
                <td>
                    Product Title :
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtProductTitle" MaxLength="20" ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Product Description :
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtProductDescription"  TextMode="MultiLine"></asp:TextBox>
                    <asp:RegularExpressionValidator runat="server" ID="REVProductDescription" ControlToValidate="txtProductDescription"
                        ValidationExpression="^[\s\S]{0,50}$" style="color:Red;font-weight:bold" ErrorMessage="Please enter a maximum of 50 characters"
                        Display="Dynamic"></asp:RegularExpressionValidator>
                </td>
            </tr>
        </table>

Output : 
(To view original image , click on image)

For Beginning .Net articles. Click Here...
 
Here is very useful .Net Tips.


Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.