Validate email format - RegularExpressionValidator

In asp.net in order to validate email address format we can use RegularExpressionValidator. We will see how to use RegularExpressionValidator to validate the email address format.For that we need to specify the Regular expression or regex in ValidationExpression property of the RegularExpressionValidator. Also we need to specify few other properties like ErrorMessage to display error message when email address is invalid. also we need to specify ControlToValidate property where we have to give textbox controls name in which user enters email address.

Validate email address using regular expression validator

Example to validate email address using RegularExpressionValidator

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Validate email format </title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:TextBox ID="txtEmail" runat="server" />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
    <asp:RegularExpressionValidator ID="validateEmail" runat="server" ErrorMessage="Invalid email."
      ControlToValidate="txtEmail" ValidationExpression="^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$" />
  </div>
  </form>
</body>


Also we need to check !Page.IsValid in event hadler of control like button or link...etc like
  protected void btnSubmit_Click(object sender, EventArgs e)
    {
      if (!Page.IsValid)
        return;
    }

Remove special characters - jQuery

In this post we will see how to remove special characters like !,@,$,*,^ ...etc. from the string. For that we need to specify the special characters in regular expressions. Then by passing specified regular expression to .replace method in jquery and replace it with '' blank.

remove special characters from string in jquery

Example to remove special characters from string - jquery

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <title>Remove special character fromstring</title>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#Send").click(function () {
                var originalstring = $("#txtinput").val();
                var filteredString = originalstring.toLowerCase().replace( /[\*\^\'\!\@\$]/g , '');
                alert(filteredString);
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox runat="server" ID="txtinput"></asp:TextBox>
    <asp:Button runat="server" ID="Send" Text="Send" />
    </div>
    </form>
</body>
</html>


We can use regular expressions to define the special character set that we need to remove from string. so if you want to remove * from the string the regular expression will be /[\*]/g. Now if you want to remove ! also the add ! to regular expression like /[\*\!]/g and so on.

c# regex for email address

In c# while dealing with email we need to validate email address. In order to validate email address we just check whether the email address format is correct or not. In C# we can use System.Text.RegularExpressions namesapce to match the email address for validation. In Regular Expressions namespace we have Regex class and from which .Match() method can be used to match the email address with the regular express defined using Regex class.

c# regex for email address

C# code to validate email format.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>C# validate email format regex</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox runat="server" ID="txtemail"></asp:TextBox>
        <asp:Button runat="server" ID="Send" OnClick="Send_Click" Text="Send" />
    </div>
    </form>
</body>
</html>

using System;
using System.Text.RegularExpressions;
public partial class Csharpregularexpressionemail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Send_Click(object sender, EventArgs e)
        {
            ValidateEmail();
        }
        private void ValidateEmail()
        {
            string email = txtemail.Text;
            Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            Match match = regex.Match(email);
            if (match.Success)
                Response.Write(email + " is corrct");
            else
                Response.Write(email + " is incorrct");
        }
    }


In above example we have specified format for email regex using constructure call of Regex class. Then we have used .Match method to match the regular express pattern for email to match. And after that we have checked if match is successfull or not.

.parent() - Get the parent of element jQuery

Get the parent of element
In web applications while working with clientside we may need to get parent element of elected element. jQuery provides function .parent() that we can use to easily get parent of the element.

Here we will see example where we have link inside div and on clicking on link we will get it's parent div.

Code to get parent of element [jQuery]

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
     <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <title>Get parent of element</title>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#submitbtn").click(function () {
                var parent = $(this).parent();
                alert(parent.text());
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="border:1px blue solid; background-color:seashell; width:200px; height:100px;">
       This is parent
       <br/>
       <input type="button" id="submitbtn" value="Click Me"/>
    </div>
    </form>
</body>
</html>

Remove css style dynamically in jquery

remove css style dynamically in jquery
Here we will see how to remove css style of the element in jquery. In web applications we need to remove css style dynamically to give user friendly ui effects and for that we can use .removeClass() function in jquery. Here we will see example of div in which when user clicks on link it will change color of div by applying css style dynamically.

Example to remove css style dynamically

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <title>Remove css style dynamically</title>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#clickme").click(function () {
                $("#dynamic-style").removeClass("myStyle");
            });
        });
    </script>
    <style type="text/css">
        .myStyle
        {
            width: 500px;
            height: 300px;
            background-color: wheat;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <a href="#" id="clickme">Change</a>
    <div id="dynamic-style" class="myStyle">
        Click change to change color.
    </div>
    </form>
</body>
</html>

Add css style dynamically in jquery

add css style dynamically injquery

In this post we will see how to add css style to element in jquery. In web applications we need to add css style dynamically to give user friendly ui effects and for that we can use .addClass() function in jquery. Here we will see example of div in which when user clicks on link it will change color of div by applying css style dynamically.

Example to add css style dynamically

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <title>Add css style dynamically</title>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#clickme").click(function () {
                $("#dynamic-style").addClass("newStyle");
            });
        });
    </script>
    <style type="text/css">
        .newStyle
        {
            width: 500px;
            height: 300px;
            background-color: wheat;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <a href="#" id="clickme">Change</a>
    <div id="dynamic-style">
        Click change to change color.
    </div>
    </form>
</body>
</html>

Get html control value in code behind without runat = server

access html control value in codebehind without runat server
In asp.net application we need to access value of html control in code behind without runat="server". For that purpose we can use name property of control. And Request.Form we can get value to code behind.So if we have html text input and you want to pass value of html input to code behind set it name property like <input name="username" />  and access it using Rquest.Form["usename"] you will get value to code behind of control without runat="server".

Code to get html control value to code behind without runat server

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
  <script type="text/javascript">
      function SubmitForm() {
         var theform;
          if (window.navigator.appName.toUpperCase().indexOf("NETSCAPE") > -1) {
              theform = document.forms["form1"];
          }
          else {
              theform = document.forms.form1;
          }
          theform.__EVENTTARGET = "btn";   
          theform.__EVENTARGUMENT = "";
          theform.submit();
      }    
</script>  
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="text" id="username" name="username" />
        <input type="button" id="btn" onclick="SubmitForm()"  value="GO"/>
    </div>
    </form>
</body>
</html>


Code behind to access html control value


using System;

namespace aspnetcontrol
{
    public partial class Getvaluewithoutrunatservercontrol : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string value = Request.Form["username"];
            Response.Write(value);
        }
    }
}

Get gridview checkbox value jQuery

get gridview selected checkboxes in jquery
In asp.net gridview we show checkboxes to select rows. Some times we need to get checked checkboxes in jQuery to perform some client side operations. in jQuery we need to first get object of gridview and then need to find checked checkboxes inside gridview.

Example to get gridview checked checkboxes in jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js"></script>
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/i18n/jquery-ui-i18n.min.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/ui-lightness/jquery-ui.css" type="text/css"/>
    <title>Get checkboxes in gridview using jQuery</title>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#submit").click(function () {
               var $checkedCheckBoxes = $('#<%=GridView1.ClientID %>').find("input:checkbox:checked");
            });
        });
      
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderWidth="1px"
            CellPadding="3" BorderStyle="None" Font-Names="Arial">
            <FooterStyle></FooterStyle>
            <PagerStyle HorizontalAlign="Left"></PagerStyle>
            <HeaderStyle Font-Bold="True"></HeaderStyle>
            <Columns>
                <asp:TemplateField HeaderText="Select">
                   <ItemTemplate>
                        <asp:CheckBox ID="CheckBoxPurchase" runat="server" Enabled="true" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Item No." DataField="ItemNo">
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                </asp:BoundField>
                <asp:BoundField HeaderText="Item Name" DataField="ItemName">
                    <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
                </asp:BoundField>
            </Columns>
        </asp:GridView>
        <input type="button" id="submit" value="Submit"/>
    </div>
    </form>
</body>
</html>

Jquery- Get hiddenfield value in user control

get value of dynamic hiddenfield
In this post we will see how to get value of dynamically loaded hiddenfield value in jQuery. This happens when we use user control in page. In such cases id of hiddenfield get changed and so hiddenfield is not accessible from javascript or jQuery. We can't access hiddenfield using class selector also because hidden field don't have class property. In this case we can use "ClientIDMode" property and set it to ClientIDMode="Static". When we set ClientIDMode="Static" it will not set dynamic id to hidden field. And now we can access it in regular way.

Code to get hiddenfield value in user control [jQuery]

You can set ClientIDMode property of hidden field in user control like:

<asp:HiddenField runat="server" ID="hiddenValue" ClientIDMode="Static" value="Some Value" />

And in jQuery you can access it as like:
   <script type="text/javascript">
        $(document).ready(function () {
            $("#submitbtn").click(function () {
                var hiddenValue = $("#hiddenValue").val();
                alert(hiddenValue);
            });
        });
    </script>

Summary: After setting ClientIDMode="Static" it will not assing dynamic id to hiddenfield and we can access hiddenfield as usual.

Get textarea value in jQuery

textarea value using jQuery

In web applications while using textarea control, we need to get the value of textarea at client side. In this post we will see example of how to get value of textarea in jQuery.

Example to get textarea value in jQuery

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="development-bundle/jquery-1.7.1.js" type="text/javascript"></script>
<script src="js/jquery-1.7.1.min.js" type="text/javascript"></script>
<title>Get textarea value in jQuery</title>
<script type="text/javascript">
$(document).ready(function () {
$("#submitbtn").click(function () {
var textAreaValue = $("#txtMessage").text();
alert(textAreaValue);
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<textarea cols="50" rows="5" id="txtMessage"></textarea>
<input type="button" id="submitbtn" value="Submit" />
</div>
</form>
</body>
</html>