Friday, March 20, 2009

Regular Expressions















Regular Expressions

CharacterDefinition
^Start of a string.
$End of a string.
.Any character (except \n newline)
|Alternation.
{...}Explicit quantifier notation.
[...]Explicit set of characters to match.
(...)Logical grouping of part of an expression.
*0 or more of previous expression.
+1 or more of previous expression.
?0 or 1 of previous expression; also forces minimal matching when an expression might match several strings within a search string.
\Preceding one of the above, it makes it a literal instead of a special character. Preceding a special matching character, see below.

















Character Classes http://tinyurl.com/5ck4ll

Char ClassDescription
.Matches any character except \n. If modified by the Singleline option, a period character matches any character. For more information, see Regular Expression Options.
[aeiou]Matches any single character included in the specified set of characters.
[^aeiou]Matches any single character not in the specified set of characters.
[0-9a-fA-F]Use of a hyphen (–) allows specification of contiguous character ranges.
\p{name}Matches any character in the named character class specified by {name}. Supported names are Unicode groups and block ranges. For example, Ll, Nd, Z, IsGreek, IsBoxDrawing.
\P{name}Matches text not included in groups and block ranges specified in {name}.
\wMatches any word character. Equivalent to the Unicode character categories
[\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \w is equivalent to [a-zA-Z_0-9].
\WMatches any nonword character. Equivalent to the Unicode categories [^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \W is equivalent to [^a-zA-Z_0-9].
\sMatches any white-space character. Equivalent to the Unicode character categories [\f\n\r\t\v\x85\p{Z}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \s is equivalent to [ \f\n\r\t\v].
\SMatches any non-white-space character. Equivalent to the Unicode character categories [^\f\n\r\t\v\x85\p{Z}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \S is equivalent to [^ \f\n\r\t\v].
\dMatches any decimal digit. Equivalent to \p{Nd} for Unicode and [0-9] for non-Unicode, ECMAScript behavior.
\DMatches any nondigit. Equivalent to \P{Nd} for Unicode and [^0-9] for non-Unicode, ECMAScript behavior.

Monday, February 23, 2009

Custom Validator to validate Date

This is a custom validation that tests if the given date is in 30 days from today. (This validator throws error when the date is less than today or when the date is greater than 30 days from today)

--------------------------------------------------------------------------------------
This post is purely for my personal reference. :-)
-----------------------------------------------------------------------------------------


<%@ 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 language="javascript" type="text/javascript">
function datevalidate(sender, args)
{
var dd = document.getElementById("<%=TextBox1.ClientID%>").value;
var mm = document.getElementById("<%=TextBox2.ClientID%>").value;
var yy = document.getElementById("<%=TextBox3.ClientID%>").value;
//Why can't I take date from Javascript ? (I want server's date as per my requirement)
var ddnow = document.getElementById("<%=hdnDay.ClientID%>").value;
var mmnow = document.getElementById("<%=hdnMonth.ClientID%>").value;
var yynow = document.getElementById("<%=hdnYear.ClientID%>").value;
mm = mm-1; // javascript month range 0 - 11
mmnow = mmnow-1; //Month ranges from 0-11 in Javascript
var tempDate = new Date(yy,mm,dd);
var today = new Date(2009,1,23);
var d = new Date();
d.setFullYear(yynow,mmnow,ddnow+30);
//First check if the given date is valid.
if ( (yy == tempDate.getFullYear()) && (mm == tempDate.getMonth()) && (dd == tempDate.getDate()) )
{
if(tempDate < today)
{
args.IsValid = false;
}
else
{
if(tempDate < d)
args.IsValid = true;
else
args.IsValid = false;
}
}
// if ( (yy == tempDate.getFullYear()) && (mm == tempDate.getMonth()) && (dd == tempDate.getDate()) )
// {
// args.IsValid=true;
// } else
// {
// args.IsValid=false;
// }
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:textbox id="TextBox1" runat="server"></asp:TextBox>
<asp:textbox id="TextBox2" runat="server"></asp:TextBox>
<asp:textbox id="TextBox3" runat="server"></asp:TextBox>
<asp:customvalidator id="custdate" runat="server" controltovalidate="TextBox3" clientvalidationfunction="datevalidate" errormessage="Invalid date"></asp:CustomValidator>
<br />
<asp:button id="Button1" runat="server" text="Button">
<input type="hidden" id="hdnDay" runat="server">
<input type="hidden" id="hdnMonth" runat="server">
<input type="hidden" id="hdnYear" runat="server">
</div>
</form>
</body>
</html>


-----------------------------------------------------------------
Code-behind
-----------------------------------------------------------------


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
hdnDay.Value = DateTime.Now.ToString("dd");
hdnMonth.Value = DateTime.Now.ToString("MM"); //m is for minutes. M is for month.
hdnYear.Value = DateTime.Now.ToString("yyyy");
}
}