Tuesday, May 17, 2011

Make Regular Expression Checker in 10 minutes using Asp.net C#

In most of the times we need to make or create a regular expression to find matching patterns from a given string or a file. Many languages support this feature like Javascript and also Asp.net C#. Regular expresion makes our life easy. Few days ago i have done a dataminig project where i found that how much necessary the regular expression is. I understand its power and capability. The developer who wants to learn regular expression then first gather some knowledge on regular expression and then use this easy and simple tool to test the pattern matches.It will definitely increases your confidence as well as skills on Regular Expression. So the regular expression example in back & forth.

The output looks like:
Regular Expression Checker

Design the UI in the following way:
<table border="0">
        <tr>
            <td>Regular Expression: </td><td><asp:TextBox ID="txtExp" runat="server" Width="200px"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Contents: </td><td><asp:TextBox ID="txtContent" runat="server" Columns="40" TextMode="MultiLine" Rows="5"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Result: </td><td><asp:TextBox ID="txtResult" runat="server" Width="200px"></asp:TextBox></td>
        </tr>
        <tr>
            <td></td><td><asp:Button ID="cmdExecute" runat="server" Text="Execute" OnClick="cmdExecute_Click" /></td>
        </tr>
    </table>
Now under Execute button click event write the following server code:
protected void cmdExecute_Click(object sender, EventArgs e)
    {
        string content = txtContent.Text;
        string pattern = txtExp.Text;
        MatchCollection mc = Regex.Matches(content, pattern);
        string sWord = "";
        if (mc.Count > 0)
        {
            for (int i = 0; i < mc.Count; i++)
            {
                if (sWord.Length == 0)
                    sWord = mc[i].Value;
                else
                    sWord =sWord+ ","+ mc[i].Value;
            }
        }
        txtResult.Text = sWord;
    }
Hope now you can test regular expressions in many different ways.

Thursday, May 12, 2011

Jquery to generate Random Number

In many forums i found a question "How to generate a Random Number using Jquery?". Its easy to generate a Random number because Jquery provides us builtin Math library function Math.random() to generate the number. To get the output you have to put a number range. Then the the mehod Math.random() will give you a Random Number. You can also use this technique for dice, random image script, or random link generator.

I have tried to produce the below output:

Jquery Random Number

To do that add an asp.net aspx page and write the below code under form tag:
<asp:Button ID="Button1" runat="server" Text="Generate Random Number" />
    <div id="divNumber">
        
    </div>

Now under head tag write the below JQuery function or method:
<script type="text/javascript">
        $(document).ready(function() {
            $("#Button1").click(function() {
                var Random_Number = Math.ceil(Math.random()*500); // Generate random number between 1 and 500               
                $("#divNumber").append("<b>The Number is: </b>"+Random_Number+"</br>");
                return false;
            });
        });
    </script>
The complete markup code should be:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Jquery_RandomNumber.aspx.cs" Inherits="Jquery_RandomNumber" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Jquery to generate Random Number</title>
    <script src="Script/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#Button1").click(function() {
                var Random_Number = Math.ceil(Math.random()*500); // Generate random number between 1 and 500               
                $("#divNumber").append("<b>The Number is: </b>"+Random_Number+"</br>");
                return false;
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="Button1" runat="server" Text="Generate Random Number" />
    <div id="divNumber">
        
    </div>

    </div>
    </form>
</body>
</html>
Now run the example and hope you can generate random number using JQuery.

Wednesday, May 11, 2011

Create generate on the fly JQuery dynamic content

In many cases developers need to add dynamic contents in a DIV. In my lattest project i have done a critical job. In this post i am sharing just a simple example on how to add dynamic contents in a DIV. By following this example you can generate on the fly dynamic contents, controls like Button, Link etc. The output look like below:

Jquery Dynamic Content



To do this fisrt add an asp.net aspx page. Then paste the below code under body tag:
<asp:Button ID="Button1" runat="server" Text="Add Dynamic Contents" />
    <div id="divDynamic">
    </div>
Now under head tag paste the below JQuery code:
<script type="text/javascript">
        $(document).ready(function() {
            $("#Button1").click(function() {               
                $("#divDynamic").append("<b>This is a dynamic content--JQUERY</b></br>");
                return false;
            });
        });
    </script>

The complete code will be:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dynamincontents_jquery.aspx.cs" Inherits="Dynamincontents_jquery" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Jquery to add dynamic contents in a DIV</title>
    <script src="Script/jquery.js" type="text/javascript"></script>
    
    <script type="text/javascript">
        $(document).ready(function() {
            $("#Button1").click(function() {               
                $("#divDynamic").append("<b>This is a dynamic content--JQUERY</b></br>");
                return false;
            });
        });
    </script>
        
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:Button ID="Button1" runat="server" Text="Add Dynamic Contents" />
    <div id="divDynamic">
    </div>
    
    </div>
    </form>
</body>
</html>
Now hope one can generate dynamic contents by using JQuery power script.
Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash