Saturday, May 23, 2015

Regular Expression Validate Email Id

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
    <title>ASP.Net - Regular Expression To Validate Email Address in C#.Net</title>
</head>
<body>
    <form id="frmValidate" name="frmValidate" runat="server">
    <div>
        <table border="1" cellpadding="5">
            <tr>
                <td>
                    Enter Email Id Here
                </td>
                <td>
                    <asp:TextBox ID="txtEmailId" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="btnValidateEmail" runat="server" Text="Check" OnClick="btnValidateEmail_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <p style="color: Red;">
                        <asp:Label ID="lblValidEmail" runat="server"></asp:Label></p>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

 C#.Net Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class ValidateEmail : System.Web.UI.Page
{
    protected void btnValidateEmail_Click(object sender, EventArgs e)
    {
        //Check Email Id is valid
        if (IsValidEmailId(txtEmailId.Text))
        {
            lblValidEmail.Text = txtEmailId.Text + " is valid email";
        }
        else
        {
            lblValidEmail.Text = txtEmailId.Text + " is not valid email";
        }
    }
    //This function returns true if the emailid matches the pattern. Else it will return false
    private bool IsValidEmailId(string InputEmail)
    {
        //Regex To validate Email Address
        Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
        Match match = regex.Match(InputEmail);
        if (match.Success)
            return true;
        else
            return false;
    }

}


Convert Rupees ie Numbers Into Words

public static string NumbersToWords(int inputNumber)
{
    int inputNo = inputNumber;

    if (inputNo == 0)
        return "Zero";

    int[] numbers = new int[4];
    int first = 0;
    int u, h, t;
    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    if (inputNo < 0)
    {
        sb.Append("Minus ");
        inputNo = -inputNo;
    }

    string[] words0 = {"" ,"One ""Two ""Three ""Four ",
            "Five " ,"Six ""Seven ""Eight ""Nine "};
    string[] words1 = {"Ten ""Eleven ""Twelve ""Thirteen ""Fourteen ",
            "Fifteen ","Sixteen ","Seventeen ","Eighteen ""Nineteen "};
    string[] words2 = {"Twenty ""Thirty ""Forty ""Fifty ""Sixty ",
            "Seventy ","Eighty ""Ninety "};
    string[] words3 = { "Thousand ""Lakh ""Crore " };

    numbers[0] = inputNo % 1000; // units
    numbers[1] = inputNo / 1000;
    numbers[2] = inputNo / 100000;
    numbers[1] = numbers[1] - 100 * numbers[2]; // thousands
    numbers[3] = inputNo / 10000000; // crores
    numbers[2] = numbers[2] - 100 * numbers[3]; // lakhs

    for (int i = 3; i > 0; i--)
    {
        if (numbers[i] != 0)
        {
            first = i;
            break;
        }
    }
    for (int i = first; i >= 0; i--)
    {
        if (numbers[i] == 0) continue;
        u = numbers[i] % 10; // ones
        t = numbers[i] / 10;
        h = numbers[i] / 100; // hundreds
        t = t - 10 * h; // tens
        if (h > 0) sb.Append(words0[h] + "Hundred ");
        if (u > 0 || t > 0)
        {
            if (h > 0 || i == 0) sb.Append("and ");
            if (t == 0)
                sb.Append(words0[u]);
            else if (t == 1)
                sb.Append(words1[u]);
            else
                sb.Append(words2[t - 2] + words0[u]);
        }
        if (i != 0) sb.Append(words3[i - 1]);
    }
    return sb.ToString().TrimEnd();
}

Below is the aspx code.
<form id="form1" runat="server">
<div>
    <table style="width: 50%;">
        <tr>
            <td>
                Input Number
            </td>
            <td>
                <asp:TextBox ID="txtNumber" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Button ID="btnConvert" Text="Convert To Words" runat="server" OnClick="btnConvert_Click" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Label ID="lblWords" runat="server"></asp:Label>
            </td>
        </tr>
    </table>
</div>
</form>