Subscribe:

Monday, 19 March 2012

Email Validation using regular expression(javascript) in Asp.net

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function validate()
    {
        var email=document.getElementById("txtemailid").value;
        var phone=document.getElementById("txtphno").value;
        var regexp1=/^([a-zA-Z0-9]+[\_\-\.]?)+@([a-zA-Z0-9]+\.)+[a-zA-Z0-9]+$/;
        var regexp2=/^[0-9]+$/;
        var Restr = /^[a-z]([\.\_]?[a-z\d]+)*\@[a-z\d]+([\.\_\-][a-z\d]+)+$/;        
        if (!regexp1.test(email))
        {
            alert("Please Enter Valid E-mail Adress");
           return false;
        } 
        else
        {
            alert("Valid Email address");
           return true;
        }
        
        if(!regexp2.test(phone))
        {          
            alert("phone number must contain only numbers");
           return false; 
        }
        if(phone.length!=10)
        {
            alert("Phone number length should be 10");
            return false;     
        }

        return true;
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style=" color:blue; font-style:normal ">  
           Email Javascript Validation
            <br /> in asp.net  
        </h2> 
    <div style="text-align:center">
    <table>
    <tr>
    <td><asp:Label ID="lblemailid" runat="server" Text="Email ID"></asp:Label></td>
    <td><asp:TextBox ID="txtemailid" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
    <td><asp:Label ID="lblphno" runat="server" Text="Phone Number"></asp:Label></td>
    <td><asp:TextBox ID="txtphno" runat="server"></asp:TextBox></td>
    </tr>
    </table>
    </div>
     <div style="text-align:center; margin-right:78%;">
     <asp:Button ID="btnsubmit" Text="Submit"  runat="server"/>
    </div>
    </form>
</body>
</html>

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        btnsubmit.Attributes.Add("onclick", "return validate();");
    }
}




Gridview Delete rows using checkboxs in asp.net

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default"  %>

<!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">
<script  type="text/javascript">
function selectAll(invoker) 
{
        var inputElements = document.getElementsByTagName('input');


        for (var i = 0 ; i < inputElements.length ; i++) 
        {

            var myElement = inputElements[i];
               if (myElement.type === "checkbox") 
               {
                myElement.checked = invoker.checked;
                }

        }
        return true;
 }    
   
   
</script>
    <title>DataGridView  Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Gridview checkBox
        <br />
        in asp.net
    </h2>
    <div>
        <table>
            <tr>
                <td>
                </td>
                <td>
                </td>
                <td>
                    <asp:Button ID="btndel" runat="server" Text="Delete" OnClick="btndel_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    <asp:GridView runat="server" ID="gridviewchk" AutoGenerateColumns="False" AllowPaging="true"
                        OnPageIndexChanging="gridviewchk_PageIndexChanging" DataKeyNames="UserID">
                        <Columns>
                            <asp:TemplateField HeaderText="ALL">
                                <HeaderTemplate>
                                    <asp:CheckBox ID="CheckBoxAll" runat="server" Text="ALL" onClick=" return selectAll(this)" />
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" />
                            <asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" />
                            <asp:BoundField DataField="counts" HeaderText="count" SortExpression="count" />
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;

public partial class _Default : System.Web.UI.Page 
{
    SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["LoginDetails"].ToString());
    
    protected void Page_Load(object sender, EventArgs e)
    {
       if(!IsPostBack)
            bind();
            
    }
   
    protected void btndel_Click(object sender, EventArgs e)
    {
        ArrayList a = new ArrayList();
        for (int i = 0; i < gridviewchk.Rows.Count; i++)
        {
            CheckBox chkbx = (CheckBox)gridviewchk.Rows[i].Cells[0].FindControl("CheckBox1");

            if (chkbx.Checked)
            {
                a.Add(gridviewchk.Rows[i].Cells[1].Text);
            }

        }

        deleterecord(a);
        bind();

    }
    public void bind()
    {
        SqlCommand cmd = new SqlCommand("select * from login", con);
       
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        gridviewchk.DataSource = ds;
        gridviewchk.DataBind();
    }
    public void deleterecord(ArrayList ar)
    {
        con.Open();
        foreach(object i in ar)
        {
            string str="delete from login where UserID='"+i+"'";
            SqlCommand cmd = new SqlCommand(str, con);
             cmd.CommandType = CommandType.Text;
     
                cmd.ExecuteNonQuery();
           
           
        }
        con.Close();
    }

    protected void gridviewchk_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gridviewchk.PageIndex = e.NewPageIndex;
        bind(); 
    }
    private void RememberOldValues()
    {
        ArrayList users = new ArrayList();
        string userid = null;
        foreach (GridViewRow row in gridviewchk.Rows)
        {
            userid= (string)gridviewchk.DataKeys[row.RowIndex].Value;
            CheckBox c = row.FindControl("CheckBox1") as CheckBox;
            bool result = c.Checked;         
            if (Session["CHECKED_ITEMS"] != null)
                users = (ArrayList)Session["CHECKED_ITEMS"];
            if (result)
            {
                if (!users.Contains(userid))
                    users.Add(userid);
            }
            else
                users.Remove(userid);
        }
        if (users != null && users.Count > 0)
            Session["CHECKED_ITEMS"] = users;
    }
    private void RePopulateValues()
    {
        ArrayList users = (ArrayList)Session["CHECKED_ITEMS"];
        if (users != null && users.Count > 0)
        {
            foreach (GridViewRow row in gridviewchk.Rows)
            {
                string userid = (string)gridviewchk.DataKeys[row.RowIndex].Value;
                if (users.Contains(userid))
                {
                    CheckBox myCheckBox = (CheckBox)row.FindControl("CheckBox1");
                    myCheckBox.Checked = true;
                }
            }
        }
    }
}



Simple login in Asp.net

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style="color: blue; font-style: normal">
        Simple login form
        <br />
        in asp.net
    </h2>
    <div>
        <table>
            <tr>
                <td>
                    <label>
                        UserName</label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <label>
                        Password</label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" TextMode="Password" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Button ID="Button1" runat="server" Text="Login" OnClick="Unnamed1_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <asp:Label ID="label1" Text="Invalid Password" runat="server" Visible="false"></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["test"].ToString());
        SqlDataAdapter da = new SqlDataAdapter("select * from login",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        int c=0;

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            string str1 = ds.Tables[0].Rows[i].ItemArray[0].ToString();
            string str2 = ds.Tables[0].Rows[i].ItemArray[1].ToString();
            if (TextBox1.Text == str1 && TextBox2.Text == str2)
            {
                Response.Redirect("Default2.aspx");
                c = 1;
            }
            
        }

        if (c == 0)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            label1.Visible = true;
        }

    }
}


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <h2 style=" color:blue; font-style:normal ">  
           SIMPLE LOGIN FORM
            <br /> in asp.net  
        </h2> 
    <div>
    Welcome to .NET World
    </div>
    </form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}