Friday, August 21, 2015

Regular Expression control - Different Expressions

links 


BThere are 3 types of brackets used in regular expression
Square brackets “[“and Curly “{“ brackets.
Square brackets specify the character which needs to be matched while curly brackets specify how many characters. “(“ for grouping.
We will understand the same as we move ahead in this article.
Ccaret “^” marks the start of a pattern.^ may appear at the beginning of a pattern to require the match to occur at the very beginning of a line. For example, ^xyz matches xyz123 but not 123xyz. 
DDollar “$” marks the end of a pattern.$ may appear at the end of a pattern to require the match to occur at the very end of a line. For example, pqr$ matches 123pqr but not pqr123. 


Caret (^) and dollar sign ($) indicate the pattern to the beginning or end of the string being searched.The two anchors may be combined. For example, ^pqr$ matches only pqr. Any characters after or before it will make the pattern invalid.  

Let’s start with the first validation, enter character which exists between a-g?

[a-g]

Enter characters between [a-g] with length of 3?

[a-g]{3}

Enter characters between [a-g] with maximum 3 characters and minimum 1 character?

[a-g]{1,3}

How can I validate data with 8 digit fix numeric format like 91230456, 01237648 etc?

^[0-9]{8}$

How to validate numeric data with minimum length of 3 and maximum of 7, ex -123, 1274667, 87654?

We need to just tweak the first validation with adding a comma and defining the minimum and maximum length inside curly brackets.
^[0-9]{3,7}$



Validate invoice numbers which have formats like LJI1020, the first 3 characters are alphabets and remaining is 8 length number?

First 3 character validation
^[a-z]{3}
8 length number validation
[0-9]{8}
Now butting the whole thing together.
^[a-z]{3}[0-9]{7}$

Check for format INV190203 or inv820830, with first 3 characters alphabets case insensitive and remaining 8 length numeric?

In the previous question the regex validator will only validate first 3 characters of the invoice number if it is in small letters. If you put capital letters it will show as invalid. To ensure that the first 3 letters are case insensitive we need to use ^[a-zA-Z]{3} for character validation.
Below is how the complete regex validation looks like.
^[a-zA-Z]{3}[0-9]{7}$



Validate numbers are between 0 to 25

^(([0-9])|([0-1][0-9])|([0-2][0-5]))$

Validation for only numbers
"^[0-9]*$" 


To get MM/DD/YYYY use the following regex pattern.
^([1-9]|0[1-9]|1[0-2])[- / .]([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])[- / .](1[9][0-9][0-9]|2[0][0-9][0-9])$
And finally to get YYYY/MM/DD use the following regex pattern.
^(1[9][0-9][0-9]|2[0][0-9][0-9])[- / .]([1-9]|0[1-9]|1[0-2])[- / .]([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])$

Short cuts

You can also use the below common shortcut commands to shorten your regex validation.
Actual commandsShortcuts
[0-9]\d
[a-z][0-9][_]\w
O or more occurrences*
1 or more occurrences+
0 or 1 occurrence?



Monday, August 17, 2015

Compare Validator Example

Design View
-------------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
        <title>CompareValidator example: how to use CompareValidator control in asp.net</title>
    </head>
   
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="Label1" runat="server"></asp:Label>
                <br />

                <asp:Label ID="Label2" runat="server" Text="<u>P</u>assword" AccessKey="P" AssociatedControlID="TextBox1"></asp:Label>
                <asp:TextBox ID="TextBox1" runat="server" TextMode="Password"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" Text="*"></asp:RequiredFieldValidator>
                <asp:CompareValidator  ID="CompareValidator" runat="server" ControlToValidate="TextBox2" ControlToCompare="TextBox1" ErrorMessage="Password does not match!">
                </asp:CompareValidator>
                <br />

                <asp:Label ID="Label3" runat="server" Text="Re-Type Password" AssociatedControlID="TextBox2"></asp:Label>
                <asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
                <br />
               

                <asp:Label ID="Label4" runat="server" Text="Data Type check"  ></asp:Label>
                <asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox3" Text="*"></asp:RequiredFieldValidator>
                <asp:CompareValidator  ID="CompareValidator1" runat="server" ControlToValidate="TextBox3" Operator="DataTypeCheck"  Type="Integer" ErrorMessage=" Give proper data type value">
                </asp:CompareValidator>
                <br />


                  <asp:Label ID="Label5" runat="server" Text="Date valuek"  ></asp:Label>
                <asp:TextBox ID="TextBox4" runat="server" ></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBox4" Text="*"></asp:RequiredFieldValidator>
                <asp:CompareValidator  ID="CompareValidator2" runat="server" ControlToValidate="TextBox4" Operator=DataTypeCheck  Type="Date"  ErrorMessage=" Give proper date value">
                </asp:CompareValidator>
                <br />

                <asp:Button ID="Button1" runat="server" Text="Compare" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
</html>


------------------------------------------------------------------------------------
code View
-------------

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "Form Submited and Password matched."
    End Sub

Required Field Validator Example


Design View
----------------------------------------------------------------------
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>


<body>

<h3><font face="Verdana">Simple RequiredField Validator Sample</font></h3>

<form id="Form1" runat="server">

    <table bgcolor="#eeeeee" cellpadding=10>
    <tr valign="top">
      <td colspan=3>
        <asp:Label ID="lblOutput" Text="Fill in the required fields below" ForeColor="red" Font-Names="Verdana" Font-Size="10" runat=server /><br>
      </td>
    </tr>

    <tr>
      <td colspan=3>
      <font face=Verdana size=2><b>Credit Card Information</b></font>
      </td>
    </tr>
    <tr>
      <td align=right>
        <font face=Verdana size=2>Card Type:</font>
      </td>
      <td>
        <ASP:RadioButtonList id=RadioButtonList1 RepeatLayout="Flow" runat=server>
            <asp:ListItem>MasterCard</asp:ListItem>
            <asp:ListItem>Visa</asp:ListItem>
        </ASP:RadioButtonList>
      </td>
      <td align=middle rowspan=1>
        <asp:RequiredFieldValidator id="RequiredFieldValidator1"
            ControlToValidate="RadioButtonList1"
             Display="Static"
             ErrorMessage="choose the card type"
            InitialValue="" Width="100%" runat=server ForeColor="Red">
            *
        </asp:RequiredFieldValidator>
      </td>
    </tr>
    <tr>
      <td align=right>
        <font face=Verdana size=2>Card Number:</font>
      </td>
      <td>
        <ASP:TextBox id=TextBox1 runat=server />
      </td>
      <td>
        <asp:RequiredFieldValidator id="RequiredFieldValidator2"
            ControlToValidate="TextBox1"
            Display="Dynamic"
              ErrorMessage="enter value to the cardnumber"
            Width="100%" runat=server ForeColor="Red">
           *
        </asp:RequiredFieldValidator>

      </td>
    </tr>
    <tr>
      <td align=right>
        <font face=Verdana size=2>Expiration Date:</font>
      </td>
      <td>
        <ASP:DropDownList id=DropDownList1 runat=server>
            <asp:ListItem></asp:ListItem>
            <asp:ListItem >06/00</asp:ListItem>
            <asp:ListItem >07/00</asp:ListItem>
            <asp:ListItem >08/00</asp:ListItem>
            <asp:ListItem >09/00</asp:ListItem>
            <asp:ListItem >10/00</asp:ListItem>
            <asp:ListItem >11/00</asp:ListItem>
            <asp:ListItem >01/01</asp:ListItem>
            <asp:ListItem >02/01</asp:ListItem>
            <asp:ListItem >03/01</asp:ListItem>
            <asp:ListItem >04/01</asp:ListItem>
            <asp:ListItem >05/01</asp:ListItem>
            <asp:ListItem >06/01</asp:ListItem>
            <asp:ListItem >07/01</asp:ListItem>
            <asp:ListItem >08/01</asp:ListItem>
            <asp:ListItem >09/01</asp:ListItem>
            <asp:ListItem >10/01</asp:ListItem>
            <asp:ListItem >11/01</asp:ListItem>
            <asp:ListItem >12/01</asp:ListItem>
        </ASP:DropDownList>
      </td>
      <td>
        <asp:RequiredFieldValidator id="RequiredFieldValidator3"
          ControlToValidate="DropDownList1"
          Display ="Static"  ErrorMessage="Select the expiration date"
          InitialValue="" Width="100%" runat=server ForeColor="Red">
          *
        </asp:RequiredFieldValidator>
      </td>
      <td>
    </tr>
 
    <tr>
      <td></td>
      <td>
        <ASP:Button id=Button1 text="Validate"  runat=server />
     

     
      </td>
      <td></td>
    </tr>
    <tr>
    <td>
 
    <asp:DropDownList id="ListFormat" AutoPostBack=true OnSelectedIndexChanged="ListFormat_SelectedIndexChanged" runat=server >
    <asp:ListItem>List</asp:ListItem>
    <asp:ListItem selected>Bulleted List</asp:ListItem>
    <asp:ListItem>Single Paragraph</asp:ListItem>
</asp:DropDownList>
    </td>
    </tr>
    <tr>
    <td colspan="2">
    <asp:ValidationSummary ID="valSum" runat="server" DisplayMode="BulletList"
                HeaderText="You must enter a value in the following fields:"
                Font-Names="verdana"
                Font-Size="12"
                />
    </td>
    </tr>
    </table>

    <br />
    Dynamic : not take any space at run time when validataion succeeds<br />
    Static : Take space at runtime even the validation succeeds<br />
    InitialValue : doesn&#39;t consider as value specified at the control need to give
    another value apart from initial value</form>

</body>
</html>
-------------------------------------------------------------------------------------------

Code behind :
-----------------

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Validate()
        End If
    End Sub

   

    Sub ListFormat_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

        ' Change display mode of the validator summary when a new option
        ' is selected from the "ListFormat" dropdownlist

        valSum.DisplayMode = ListFormat.SelectedIndex
    End Sub



Thursday, August 6, 2015

Creating Advertisement using Adrotator control in ASP.NET

Step 1:  Add new item - XML file to the web page

Choose the xml file to write below coding for Advertisement
Copy the below code and paste it in the XML file and also include the relevent images which is specified name in your code ( Add existing item -> include appropriate images)


------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
  <Ad>
    <ImageUrl>rose1.jpg</ImageUrl>
    <NavigateUrl>http://www.1800flowers.com</NavigateUrl>
    <AlternateText>
      Order flowers, roses, gifts and more
    </AlternateText>
    <Impressions>10</Impressions>
    <Keyword>flowers</Keyword>
    <Width>1000</Width>
    <Height>150</Height>
  </Ad>

  <Ad>
    <ImageUrl>rose2.jpg</ImageUrl>
    <NavigateUrl>http://www.babybouquets.com.au</NavigateUrl>
    <AlternateText>Order roses and flowers</AlternateText>
    <Impressions>10</Impressions>
    <Keyword>gifts</Keyword>
    <Width>1000</Width>
    <Height>150</Height>
  </Ad>

  <Ad>
    <ImageUrl>rose3.jpg</ImageUrl>
    <NavigateUrl>http://www.flowers2moscow.com</NavigateUrl>
    <AlternateText>Send flowers to Russia</AlternateText>
    <Impressions>10</Impressions>
    <Keyword>russia</Keyword>
    <Width>1000</Width>
    <Height>150</Height>
  </Ad>

  <Ad>
    <ImageUrl>rose4.jpg</ImageUrl>
    <NavigateUrl>http://www.edibleblooms.com</NavigateUrl>
    <AlternateText>Edible Blooms</AlternateText>
    <Impressions>10</Impressions>
    <Keyword>gifts</Keyword>
    <Width>1000</Width>
    <Height>150</Height>
  </Ad>
</Advertisements>
----------------------------------------------------------
These are all the description of  Standard XML tags for creating Advertisements in Webpage.
Element Description
Advertisements Encloses the advertisement file.
Ad Delineates separate ad.
ImageUrl The path of image that will be displayed.
NavigateUrl The link that will be followed when the user clicks the ad.
AlternateText The text that will be displayed instead of the picture if it cannot be displayed.
Keyword Keyword identifying a group of advertisements. This is used for filtering.
Impressions The number indicating how often an advertisement will appear.
Height Height of the image to be displayed.
Width Width of the image to be displayed.
-------------------------------------------------------------------------------------


Added images to the existing application:


Step 2:  In the design / Source view copy the below code or just drag and place the Adrotator control


        <asp:AdRotator ID="AdRotator2" runat="server" AdvertisementFile="~/ADXMLFile.xml" />

here the  AdvertisementFile attribute which specifies the XML file we have created to dentoe the various images with their corresponding attributes.


Step 3:
      
              Then run the page press "F5" . At the initial stage you will be see the singe image for the very first time. then  Refreshing the page you may able to see the random of images according to impression you have given to the particular image.


The above steps can be used to display random Advertisement can be shown when the page is Refreshed. If we want to show the Advertisement Randomly without refreshing the page means should use with Ajax enabled controls like Script manager , Update Panel etc.....

Just copy the below code in source view to see the difference:

  <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
         <asp:Timer ID="Timer1" Interval="1000" runat="server" >
                </asp:Timer>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
             <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
        </Triggers>
            <ContentTemplate>

                <asp:AdRotator ID="AdRotator1" runat="server" AdvertisementFile="~/ADXMLFile.xml" />
              
            </ContentTemplate>
        </asp:UpdatePanel>







Saturday, August 1, 2015

HTML control Event in ASP.net Code behind and Client Script

this is for HTML control event at client side script function

just copy the below code in source page of ur asp.net application

<html xmlns="http://www.w3.org/1999/xhtml" >
    <script language="VB" runat="server">
       Sub FancyBtn_Click(Source As Object, E as EventArgs)
          Message.InnerHtml = "Your name is: " & Name.Value
       End Sub
    </script>

    <head runat="server">
    <title> Enter Name: </title>
</head>
<body>
          <form id="form1" method="post" runat="server">

            <h3> Enter Name: <input id="Name" type="text" size="40" runat="server" />
            </h3>

             <button onserverclick=" FancyBtn_Click" runat="server">
               <b><i> I'm a fancy HTML 4.0 button </i> </b>
             </button>


           <h1>
             <span id="Message" runat="server"></span>
           </h1>

          </form>
       </body>
 </html>




---------------------------------------------------------

this is for HTML control event at server side code behind function

just copy the below code in view code of ur asp.net application
   Sub FancyBtn_Click(Source As Object, E as EventArgs)
          Message.InnerHtml = "Your name is: " & Name.Value
       End Sub


but comment the  below code in htmlsource view
/*<script language="VB" runat="server">
       Sub FancyBtn_Click(Source As Object, E as EventArgs)
          Message.InnerHtml = "Your name is: " & Name.Value
       End Sub
    </script>*/