Friday, September 22, 2006

Validating User Input string length in a text box.

In this article we will see how we can validate or limit the length of the user input string.

One way is sitting the max length property of the textbox, but that will only validate the max no of characters that can be entered in the textbox. But what is we need to do a validation like the password should be minimum of 6 characters in length and should not exceed 15 characters.

 Is it possible, yes it is, we can use java script to validate this. The following is the script.

[code]

function checkCharLen()
{
if (form1.text1.value.length >=6 && form1.text1.value.length <=15)
          { return true; }
else
          { alert(“Should be 6 to 15 characters”); 
             form1.text1.focus(); return false;
          }
}

[/code]

We need to call this function in the OnBlur event of the textbox.  OnBlur event will fire when the focus is lost.

 

Posted by Sadha at 12:17:51 | Permalink | Comments (2)

Exporting a Data Grid to Excel

This article will teach you about exporting a data to excel.

In most of the application we need to export the data to excel. We need to do a lot of formatting to the data (tabular form, seeting the widht, height of the cell etc). Formattign the data using excel automation is a tedious job.

 The method which i am going to tell you now is going to be very simple. Excel can also be compared with HTML table, Excel work sheet is a matrix of rows and columns.

Generating an excel from aspx is very simple we need to format the html table with data and the change the contect type alone and print the table on the page using response.write. For changing the content type use the following code.

[code]
  Response.ClearContent()
  Response.ClearHeaders()
  Response.ContentType = “application/vnd.ms-excel”
[/code]

 Again most of us fail to format the data propoerly in a HTML table. But we all know how to work with the datagrid and also that the final rendered out put of the datagrid is HTML tags that is a table.

 So all we need is bind the dataset to the datagrid, format the datagird, then get the HTML output (render the html output) and print it. Use the following code for that.

 [code]
   Dim dg As New DataGrid
   Dim sw As New System.IO.StringWriter
   Dim htw As New System.Web.UI.HtmlTextWriter(sw)
   dg.RenderControl(htw)
   Response.Write(sw.ToString)
   Response.End()
[/code]

Is it not simple

Posted by Sadha at 11:50:33 | Permalink | Comments (1) »

Wednesday, September 20, 2006

Compare Validator

Compare Validator

 

In some case we may have to do a validation based on some of the underlying values/control data, for example.,

 

Now we need to check whether the Password and confrim password in the new user form are the same. In these cases compare validators can be used.

 

Lets develop a page which has two textboxes Password and confirm password and do a validation whether the password and Re-Enter password are the same.

 

Create a new Web form and place two textboxes (name them as txtPassword & txtConfirmPassword) and a button as shown in the picture below.

 

Now add a compare validator next to the confirm password textbox box, The select the validator control and open the property window Set the following properties.

 

  1. ErrorMessage = “Password doesn’t match”
  2. ControlToValidate = “txtConfirmpassword”
  3. ControlToCompare = “txtPassword”

 

CompareValidator Tag should belike this.

 

[code]

<asp:CompareValidator id=”cmpPassword” runat=”server” ErrorMessage=”Passwords doesn’t match” ControlToValidate=”txtConfirmPassword” ControlToCompare=”txtPassword”></asp:CompareValidator>

[/code]

 

Now let us see another example, to compare the text entered with some value. In most of the websites now we have some thing called us Picture verification, where in some random pictures are generated with some characters in it. A textbox will be given for the user to enter the characters displayed in the pic.

 

In this example, let us create similar one.

 

Design a page as shown below,

 

Add a text box and name it as txtString and add a compare validator, and set the following properties.

 

  1. ErrorMessage = “Entered String does not match”
  2. ControlToValidate = txtString
  3. ValueToCompare = “ZKdul”

 

[code]

<asp:CompareValidator id=”Comparevalidator1″ runat=”server” ErrorMessage=”Entered string is not correct” ControlToValidate=”txtString” ValueToCompare=”ZKdul”></asp:CompareValidator>

[/code]

 

Posted by Sadha at 12:50:26 | Permalink | Comments (1) »

Monday, September 18, 2006

Working with validators in ASP.NET

Working with validators

 

In any application, validating the user input is very important; it is process of the identifying what is incorrect in the user inputs. It is used to identify the errors in the user input before carrying out the business process.

 

Take for example; we are going to create a new email-using Yahoo. Where in the user id has to be given before submitting the form. This is one kind of validation. There are several other cases like the password should not exceed 10 characters or Date of birth entered should not exceed the current date or the age should be in the between a range say (10 to 50 or above 10 etc) or even the format in which the data are given.

  

Validators in ASP.NET

 

In this article lets discuss about the validators and its kinds in ASP.Net.

 

There are five different kinds of validators available in .net, they are as follows,

 

  • Required Field validators
  • Compare Validators
  • Range Validators
  • RegularExpression validators
  • Custom Validators

 

Let us first see what each validators is meant for.

 

Required Field Validators

 

The name itself describes it, yes it is the validator used to check whether the user has entered/selected a value or not.

 

[code]

<asp:RequiredFieldValidator id="valReq" runat="server" ControlToValidate=”"
    ErrorMessage="* You must enter a value" Display="dynamic">

</asp:RequiredFieldValidator>

[/code]

 

Compare Validators

 

In some case we may have to do a validation based on some of the underlying values/control data, for example.,

 

Now we need to check whether the Password and Re-Enter password in the new user form are the same. In these cases compare validators can be used.

 

[code]
<asp:CompareValidator id="valCom" runat="server"
    ControlToValidate="textbox1" ControlToCompare="textbox2"
    Operator="Equals"
    ErrorMessage=”"
    Display="dynamic">*

</asp:CompareValidator>[/code]  

Range Validator Control

 

In most of the case we may have to check whether the data entered is between a specified range. For example, the age should be minimum 10 and maximum 100.

 

In those cases range validators can be used.

 

[code]

<asp:RangeValidator id="valRange" runat="server"
    ControlToValidate="age"
    MaximumValue="100"
    MinimumValue="10"
    Type="integer"

    ErrorMessage=“* The age must be between 10 and 100″ Display=“static”></asp:RangeValidator> [/code] 

Regular Expression validator

 

It is one of important validators available in Asp.net it is used to compare the data entered against specific format. For example date has to be entered in dd/MM/yyyy format only.

 

[code]

<asp:RegularExpressionValidator id="valReg" runat="server"
    ControlToValidate=""
    ValidationExpression=""
    ErrorMessage=""
    display="dynamic">

</asp:RegularExpressionValidator>

[/code]

 
Custom Validators 

In all the cases the above validators cannot be used, that is some validations cannot be done on the page, it has to come for round trips to the server validate the data in the server against some condition or some data base values and then display error messages based on that.

 

For example, checking whether the user name already exists or not.

 

<asp:CustomValidator id="valCustom" runat="server"
    ControlToValidate="textbox1"
    ClientValidationFunction="ClientValidate"
    OnServerValidate="ServerValidate"
    ErrorMessage=" " dispaly="dynamic">*

</asp:CustomValidator>

 

We will see in depth of these validators in our next chapters.

 

Now lets see what are the attributes are methods available in common

 Common Properties and Methods of validators. 

ControlToValidate –

 

Take a string input, name of the control to be validated.

 

ErrorMessage

 

The error message that needs to be displayed in case if the validations fails (some error in the data)

 

IsValid

 

Returns true if the control has valid date, else returns false.

 

Validate

 

Method to validate the control against the validation rule and to update the Isvalid property.

 

Display

 

There are three different ways of presenting the data or error message to the user.

 

  • None – Validation message will not be displayed at all
  • Static – Allocate space on the page to display the error message.
  • Dynamic – Space will not be allocated on the page if there is no error. Space will be dynamically added only when the validation fails.

  

Posted by Sadha at 12:28:12 | Permalink | Comments (1) »

Wednesday, September 15, 2004

A Small Tip in ASP.NET #1


Introduction

Hai everybody, i am back wiht one more tip for all you.

Asusual, this tip is also going to be simple one. That all of you might me be knowing. But most of you would have not thought how to use that. I am writing such articles, to improve your thinking. Every thing has something in it, we should explore it and try to use it differently whenever possible.

Ok now I am going to tell “How to show an Message Box ?“, I am not going to use any new function, I am just going to use Java Script alert method and display the message box.

How to use the alert method in ASP.NET

There are two methods of using this alert method in ASP.Net.

  • Using Response.Write
  • Using Page object

Click here to read more…….


Posted by Sadha at 13:19:09 | Permalink | Comments (2)