Sponsored Ad

Thursday, June 24, 2010

How to Validate Zip Code using RegularExpressionValidator in ASP.NET

RegularExpressionValidator is very useful control to validate any custom logic. For example in this sample code, 5 digit zip code validation is implemented using asp.net.

So whenever user enters the input other than 5 digits. It prompts for error.

 

How to Validate Zip Code using RegularExpressionValidator in ASP.NET

How to Validate Zip Code using RegularExpressionValidator in ASP.NET

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Check for 5 digit zip code</h4>
        <br />
        Enter a zip code of 5 digits:
        <asp:TextBox ID="txtRegularExp" runat="server"></asp:TextBox>
        <br />
        <br />
        <asp:Button ID="Button1" Text="Validate" runat="server" />
        <br />
        <br />
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtRegularExp"
            ValidationExpression="\d{5}" ErrorMessage="Please enter 5 digit zip code" runat="server" />
    </form>
</body>
</html>

Saturday, June 19, 2010

How to Check/Compaire/Validate DropDownList selection at client side in ASP.NET | CompareValidator

You can verify at client side that any value in dropdownlist is selected or not. Here is a simple example which have dropdown with some values and first value is “Select”. Comparevalidator checkes for non Select value in dropdown otherwise it stops the execution and promp a error message to select a value from dropdownlist. Inplace of Select if you have a blank value at first place in dropdown then youcan change the value of

ValueToCompare="Select"

To

ValueToCompare=""

 

How to Check/Compaire/Validate DropDownList selection at client side in ASP.NET | CompareValidator

How to Check/Compaire/Validate DropDownList selection at client side in ASP.NET | CompareValidator

 

 

How to Check/Compaire/Validate DropDownList selection at client side in ASP.NET | CompareValidator Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >
<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Compare Two Password Values</h4>
        <br />
        <br />
        Select a Dropdown Value:
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem>Select</asp:ListItem>
            <asp:ListItem>IndiHub.com</asp:ListItem>
            <asp:ListItem>SoftwareTestingNet.com</asp:ListItem>
        </asp:DropDownList>
        <br />
        <br />
        <asp:Button ID="Button1" Text="Validate" runat="server" />
        <br />
        <br />
        <asp:CompareValidator ID="compair_dropdown_blank_value" ControlToValidate="DropDownList1" ValueToCompare="Select"
            ForeColor="red" Type="string" Text="Please Select at least one value to proceed."
            Operator="notequal" runat="server" />
    </form>
</body>
</html>

Wednesday, June 16, 2010

How to Compaire/Validate Two Double Values in ASP.NET | CompareValidator

Easy date copaire validator is available in ASP.NET 2.0 and later versions. You just need to select type as date in CompareValidator and give name of both textboxes which contains the date.

It can compaire the date between different date formats also. Like you can see example in these two figures.

12/11/2010 and 12/11/09 are different while 12/11/2010 and 12/11/10 are same.

 

How to Compaire/Validate Two Double Values in ASP.NET | CompareValidator

How to Compaire/Validate Two Double Values in ASP.NET | CompareValidator

 

 

How to Compaire/Validate Two Double Values in ASP.NET | CompareValidator Example:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >
<html>
<body>
    <form id="Form1" runat="server">
        <h4>
            Compare Two Date Values</h4>
       Date Value 1:
        <asp:TextBox ID="txtDate1" runat="server" />
        <br />
        <br />
        Date Value 2:
        <asp:TextBox ID="txtDate2" runat="server" />
        <br />
        <br />
        <br />
        <asp:Button ID="Button1" Text="Validate" runat="server" />
        <br />
        <br />   
        <asp:CompareValidator ID="compair_Date_values"  ControlToValidate="txtDate1" ControlToCompare="txtDate2"
            ForeColor="red" Type="Date"  Text="Please enter same Date values in both textboxes."
            runat="server" />
    </form>
</body>
</html>

Sunday, June 13, 2010

How to Validate Credit Card in C#

While wi=orking with financial applications, sometimes we need to validate creadit card number. You can easily validate creadit card number.

This C# code block will check the validity for

  1. MasterCard
  2. American Express
  3. Visa

Cards. So just copy this code and pass the card type and card number. This function will return true if card is valid and false if card is invalid.

 

How to Validate Credit Card in C# Example

public bool Validate_Credit_Card(string card_type, string card_number)
    {
        byte[] bNumber = new byte[16];
        int intLength = 0;
        for (int i = 0; i < card_number.Length; i++)
        {
            if (char.IsDigit(card_number, i))
            {
                if (intLength == 16) return false;
                bNumber[intLength++] = byte.Parse(card_number[i].ToString());
            }

        }

        switch (card_type)
        {
            case "MasterCard":
                if (intLength != 16)
                    return false;
                if (bNumber[0] != 5 || bNumber[1] == 0 || bNumber[1] > 5)
                    return false;
                break;

            case "AmericanExpress":
                if (intLength != 15)
                    return false;
                if (bNumber[0] != 3 || (bNumber[1] != 4 && bNumber[1] != 7))
                    return false;
                break;

            case "Visa":
                if (intLength != 16 && intLength != 13)
                    return false;
                if (bNumber[0] != 4)
                    return false;
                break;
        }

        int sum = 0;
        for (int i = intLength - 1; i >= 0; i--)
        {
            if (i % 2 == intLength % 2)
            {
                int n = bNumber[i] * 2;
                sum += (n / 10) + (n % 10);
            }
            else
                sum += bNumber[i];
        }
        if (sum == 0)
        {
            return false;

        }
        else
        {
            return (sum % 10 == 0);
        }

    }

Sunday, June 6, 2010

Response.Flush() in ASP-C#

This method is used to send output immediately to screen. Please note that Buffer shuould be true before calling flush() method.

 

Response.Flush() in ASP-C# Example:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" >

<%
Response.Buffer=true;
%>
<html>
<body>
<p>
The flush method send buffered output immeditly.
</p>
<%
    Response.Flush();
%>

</body>
</html>

Thursday, June 3, 2010

How to use Monitor in multithreaded application – C#

How to use Monitor in multithreaded application – C#

 

Monitor is use to maintain the critical section in multithreaded application.

Critical section is a code section where only one thread can execute the code at atime.

Monitor.Enter(this);

Code----

Monitor.Exit(this);

This statement enter the current thread into the critical section of current object “this”. Once the execution is over for current thread the another thread will get a chance to execute the same code.

Monitor.TryEnter(this);

Code----

Monitor.Exit(this);

In The above block of code TryEnter will work same as Enter but it returns the boolien values. For the first thread it will return true and enter critical section. For the second thread it will return false and enter in critical section.

Monitor.TryEnter(this,500);

Specifying time with Monitor.TryEnter function means it will block thread for 500 miliseconds.

 

How to use Monitor in multithreaded application – C# Example:

using System;
using System.Text;
using System.Threading;

namespace Console_App
{

    public class Class1
    {
        public void fun1()
        {
            Monitor.TryEnter(this,500);

            for (int i = 0; i < 5; i++)
            {
                Thread.Sleep(500);
                Console.WriteLine(" #" + i + " " + Thread.CurrentThread.GetHashCode());

            }
            Monitor.Exit(this);

        }    
    }
    public class Class2
    {
        public static void Main()
        {
            Class1 a = new Class1();
            Thread thread1 = new Thread(new ThreadStart(a.fun1));
            Thread thread2 = new Thread(new ThreadStart(a.fun1));

            thread2.Start();
            thread1.Start();

            Console.ReadLine();
        }
    }

}

Tuesday, June 1, 2010

Thread IsAlive Property and Thread Execution

Thread IsAlive Property and Thread Execution

In this example we will discuss the thread execution and isalive property of thread. As you can check in output before starting the thread IsAlive is false and after staring thread IsAlive property is True and After Aborting the therad IsAlive property is Flase. So after aborting the thread is no more.

While executing this program sometimes you can get that even after aborting the thread IsAlive property is true. This happened because thread abort take time and the next statement executes before thread completely abort. So while programming please take care of this scenrio.

 

Thread IsAlive Property and Thread Execution Example:

using System;
using System.Text;
using System.Threading;

namespace Console_App
{

    public class Class1
    {
        public void fun1()
        {
            for (int i = 0; i < 10000; i++)
            {
                //Console.WriteLine(" #" + i );
             //   Thread.Sleep(2);
            }
        }
    }
    public class Class2
    {
        public static void Main()
        {
            Class1 a = new Class1();
            Thread thread1 = new Thread(new ThreadStart(a.fun1));
            Console.WriteLine("Before Starting Thread: " + thread1.IsAlive);
            thread1.Start();
            Console.WriteLine("After Starting Thread: " + thread1.IsAlive);
            thread1.Abort();
            Console.WriteLine("After Aborting Thread: " + thread1.IsAlive);
            Console.ReadLine();
        }
    }
}

Sponsored Ad

More Related Articles

Website Update

Followers