Tuesday, March 15, 2011

Array in Javascript

The Array object is used to store multiple values in a single variable.
An array can hold all your variable values under a single name. And you can access the values by referring to the
array name.
Each element in the array has its own ID so that it can be easily accessed.


Create an Array
An array can be defined in three ways.
The following code creates an Array object called myCars:
1:
var myCars=new Array(); // regular array (add an optional integer
myCars[0]="Saab";       // argument to control array's size)
myCars[1]="Volvo";
myCars[2]="BMW";
2:
var myCars=new Array("Saab","Volvo","BMW"); // condensed array
3:
var myCars=["Saab","Volvo","BMW"]; // literal array
Note: If you specify numbers or true/false values inside the array then the variable type will be Number or Boolean,
instead of String.


Access an Array
You can refer to a particular element in an array by referring to the name of the array and the index number. The index
number starts at 0.
The following code line:
document.write(myCars[0]);
will result in the following output:
Saab



Modify Values in an Array
To modify a value in an existing array, just add a new value to the array with a specified index number:
myCars[0]="Opel";
Now, the following code line:
document.write(myCars[0]);
will result in the following output:
Opel


*/
<html>
<body>

<script type="text/javascript">
var i;
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";

for (i=0;i<mycars.length;i++)
{
document.write(mycars[i] + "<br />");
}
</script>

</body>
</html>

// concat two array

var parents = ["Jani", "Tove"];
var children = ["Cecilie", "Lone"];
var family = parents.concat(children);
document.write(family);


*/



campare two dates in Javascript

The Date object is also used to compare two dates.

The following example compares today's date with the 14th January 2011:

/* var myDate=new Date();
myDate.setFullYear(2011,0,14);
var today = new Date();

if (myDate>today)
  {
  alert("Today is before 14th January 2011");
  }
else
  {
  alert("Today is after 14th January 2011");
  }

*/

Creating a Digital Clock in Javascript

/*

<html>

<head>

<script type="text/javascript">

function startTime()

{

var today=new Date();

var h=today.getHours();

var m=today.getMinutes();

var s=today.getSeconds();

// add a zero in front of numbers<10

m=checkTime(m);

s=checkTime(s);

document.getElementById('txt').innerHTML=h+":"+m+":"+s;

t=setTimeout('startTime()',500);

 

}

 

function checkTime(i)

{

if (i<10)

  {

  i="0" + i;

  }

return i;

}

</script>

</head>

 

<body onload="startTime()">

<div id="txt"></div>

</body>

</html>

*/

Monday, March 14, 2011

validation using javascript



/*
function validate()
{
// for check that text box is fill or empty.
     if (document.getElementById("<%=txtName.ClientID%>").value=="")

     {
                alert("Name Feild can not be blank");
                document.getElementById("<%=txtName.ClientID %>").focus();
                return false;
     }
     if(document.getElementById("<%=txtEmail.ClientID %>").value=="")
     {
                alert("Email can not be blank");
               document.getElementById("<%=txtEmail.ClientID%>").focus();
               return false;
     }
// check that email address is valid or not.
    var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
    var emailid=document.getElementById("<%=txtEmail.ClientID%>").value;
   var matchArray = emailid.match(emailPat);
    if (matchArray == null)
       {
                  alert("Your email address seems incorrect. Please try again.");
                  document.getElementById("<%=txtEmail.ClientID%>").focus();
                  return false;
       }
     if(document.getElementById("<%=txtCourse.ClientID%>").value=="")
     {
                alert("Cousrse field can not be blank");
               document.getElementById("<%=txtCourse.ClientID%>").focus();
               return false;
     }

     return true;
 }
</script> */

Saturday, March 12, 2011

Regular Expressions in C# and VB.Net

What are Regular Expressions?

Regular Expressions are a powerful pattern matching language that is part of many modern programming languages.
Regular Expressions allow you to apply a pattern to an input string and return a list of the matches within the text.
Regular expressions
also allow text to be replaced using replacement patterns. It is a very powerful version of find and replace.

There are two parts to learning Regular Expressions;
  • learning the Regex syntax
  • learning how to work with Regex in your programming language
This article introduces you to the Regular Expression syntax. After learning the syntax for Regular Expressions you can use it many different languages as the syntax is fairly similar between languages.
Microsoft's .NET Framework contains a set of classes for working with Regular Expressions in the System.Text.RegularExpressions namespace.

Download the Regular Expression Designer

When learning Regular Expressions, it helps to have a tool that you can use to test Regex patterns. Rad Software has a Free Regular Expression Tool available for download that will help as you go through the article.

The basics - Finding text

Regular Expressions are similar to find and replace in that ordinary characters match themselves. If I want to match the word "went" the Regular Expression pattern would be "went".
Text:    Anna Jones and a friend went to lunch Regex:   went Matches: Anna Jones and a friend went to lunch went
The following are special characters when working with Regular Expressions. They will be discussed throughout the article.
. $ ^ { [ ( | ) * + ? \

Matching any character with dot

The full stop or period character (.) is known as dot. It is a wildcard that will match any character except a new line (\n). For example if I wanted to match the 'a' character followed by any two characters.
Text:    abc def ant cow Regex:   a.. Matches: abc def ant cow abc ant
If the Singleline option is enabled, a dot matches any character including the new line character.

Matching word characters

Backslash and a lowercase 'w' (\w) is a character class that will match any word character. The following Regular Expression matches 'a' followed by two word characters.
Text:    abc anaconda ant cow apple Regex:   a\w\w Matches: abc anaconda ant cow apple abc ana ant app
Backslash and an uppercase 'W' (\W) will match any non-word character.

Matching white-space

White-space can be matched using \s (backslash and 's'). The following Regular Expression matches the letter 'a' followed by two word characters then a white space character.
Text:    "abc anaconda ant" Regex:   a\w\w\s Matches:  "abc "
Note that ant was not matched as it is not followed by a white space character.
White-space is defined as the space character, new line (\n), form feed (\f), carriage return (\r), tab (\t) and vertical tab (\v). Be careful using \s as it can lead to unexpected behaviour by matching line breaks (\n and \r). Sometimes it is better to explicitly specify the characters to match instead of using \s. e.g. to match Tab and Space use [\t\0x0020]

Matching digits

The digits zero to nine can be matched using \d (backslash and lowercase 'd'). For example, the following Regular Expression matches any three digits in a row.
Text:    123 12 843 8472 Regex:   \d\d\d Matches: 123 12 843 8472 123 843 847

Matching sets of single characters

The square brackets are used to specify a set of single characters to match. Any single character within the set will match. For example, the following Regular Expression matches any three characters where the first character is either 'd' or 'a'.
Text:    abc def ant cow Regex:   [da].. Matches: abc def ant cow abc def ant
The caret (^) can be added to the start of the set of characters to specify that none of the characters in the character set should be matched. The following Regular Expression matches any three character where the first character is not 'd' and not 'a'.
Text:    abc def ant cow Regex:   [^da].. Matches:  "bc " "ef " "nt " "cow"

Matching ranges of characters

Ranges of characters can be matched using the hyphen (-). the following Regular Expression matches any three characters where the second character is either 'a', 'b', 'c' or 'd'.
Text:    abc pen nda uml Regex:   .[a-d]. Matches: abc pen nda uml abc nda
Ranges of characters can also be combined together. the following Regular Expression matches any of the characters from 'a' to 'z' or any digit from '0' to '9' followed by two word characters.
Text:    abc no 0aa i8i Regex:   [a-z0-9]\w\w Matches: abc no 0aa i8i abc 0aa i8i
The pattern could be written more simply as [a-z\d]

Specifying the number of times to match with Quantifiers

Quantifiers let you specify the number of times that an expression must match. The most frequently used quantifiers are the asterisk character (*) and the plus sign (+). Note that the asterisk (*) is usually called the star when talking about Regular Expressions.

Matching zero or more times with star (*)

The star tells the Regular Expression to match the character, group, or character class that immediately precedes it zero or more times. This means that the character, group, or character class is optional, it can be matched but it does not have to match. The following Regular Expression matches the character 'a' followed by zero or more word characters.
Text:    Anna Jones and a friend owned an anaconda Regex:   a\w* Options: IgnoreCase Matches: Anna 
Jones and a friend owned an anaconda Anna and a an anaconda

Matching one or more times with plus (+)

The plus sign tells the Regular Expression to match the character, group, or character class that immediately precedes it one or more times. This means that the character, group, or character class must be found at least once. After it is found once it will be matched again if it follows the first match. The following Regular Expression matches the character 'a' followed by at least one word character.
Text:    Anna Jones and a friend owned an anaconda Regex:   a\w+ Options: IgnoreCase Matches: Anna 
Jones and a friend owned an anaconda Anna and an anaconda
Note that "a" was not matched as it is not followed by any word characters.

Matching zero or one times with question mark (?)

To specify an optional match use the question mark (?). The question mark matches zero or one times. The following Regular Expression matches the character 'a' followed by 'n' then optionally followed by another 'n'.
Text:    Anna Jones and a friend owned an anaconda Regex:   an? Options: IgnoreCase Matches: Anna Jones 
and a friend owned an anaconda An a an a an an a a

Specifying the number of matches

The minimum number of matches required for a character, group, or character class can be specified with the curly brackets ({n}). The following Regular Expression matches the character 'a' followed by a minimum of two 'n' characters. There must be two 'n' characters for a match to occur.
Text:    Anna Jones and Anne owned an anaconda Regex:   an{2} Options: IgnoreCase Matches: Anna Jones
 and Anne owned an anaconda Ann Ann
A range of matches can be specified by curly brackets with two numbers inside ({n,m}). The first number (n) is the minimum number of matches required, the second (m) is the maximum number of matches permitted. This Regular Expression matches the character 'a' followed by a minimum of two 'n' characters and a maximum of three 'n' characters.
Text:    Anna and Anne lunched with an anaconda annnnnex Regex:   an{2,3} Options: IgnoreCase Matches:
 Anna and Anne lunched with an anaconda annnnnex Ann Ann annn
The Regex stops matching after the maximum number of matches has been found.

Matching the start and end of a string

To specify that a match must occur at the beginning of a string use the caret character (^). For example, I want a Regular Expression pattern to match the beginning of the string followed by the character 'a'.
Text:    an anaconda ate Anna Jones Regex:   ^a Matches: an anaconda ate Anna Jones "a" at position 1
The pattern above only matches the a in "an".
Note that the caret (^) has different behaviour when used inside the square brackets.
If the Multiline option is on, the caret (^) will match the beginning of each line in a multiline string rather than only the start of the string.
To specify that a match must occur at the end of a string use the dollar character ($). If the Multiline option is on then the pattern will match at the end of each line in a multiline string. This Regular Expression pattern matches the word at the end of the line in a multiline string.
Text:    "an anaconda ate Anna Jones" Regex:   \w+$ Options: Multiline, IgnoreCase Matches:  Jones 

Regular Expression Elements

Some of the commonly used regular expression elements are:
^ Matches start of input
$ Matches end of input
. Matches any character except new line
| OR
* Match the preceding expression 0 or more number of times
+ Match the preceding expression 1 or more number of times
? Match the preceding expression 0 or 1 number of times
() Logical group / sub-expression (capture as auto number group)
(?(exp)) Named capture group
(?=exp) Match any position preceding a suffix exp
(?<=exp) Match any position following a prefix exp
(?!exp) Match any position after which exp is not found
(?<!--exp) Match any position before which exp is not found
[…] List of characters to match
[^expression] Not containing any of the specified character
{n} or {n. m} Quantifier (Match exact number or range of instances)
(?(exp (yes|no)) If expression (exp) is true, match yes part else no part
\ Escape character (to match any of the special characters)
\w Match any word character
\W Match any non-word character
\s Match any white space character
\S Match any non-white space character
\d Match any numeric digit
\D Match any numeric digit
\b Match a backspace if in character matching mode ([]). Otherwise match the position at beginning or end of a word
\t Match tab
\r Match carriage return
\n Match line feed
The following are matching substitutions:
num Substitute last substring matched by group number num
${name} Substitute last substring matched by group name
$& Substitute a copy of entire text itself
$` Substitute all the text of the input string before match
$' Substitute all the text of the input string after match
$+ Substitute last matched group
$_ Substitute input string
$$ Substitute literal $
Regular expressions could also be used to find repeating patterns by making use of back referencing, using which you can name a pattern found and then use that reference elsewhere in the expression. This naming of patterns is also useful in case we need to parse a string like free form date or time strings.

C# Regular Expression (Regex) Examples in .NET

Matching special characters with character escapes

Special characters such as Tab and carriage return are matched using character escapes. The syntax is similar to C and C#. The common character escapes are listed below.
Special Character Description
\t Matches a tab
\r Matches a carriage return
\n Matches a new line
\u0020 Matches a Unicode character
using hexadecimal representation.
Exactly four digits must be specified.
In this example, the Regular Expression pattern matches one or more word characters followed by a carriage return then a new line.
Text:    an anaconda ate
Anna Jones Regex:   \w+\r\n Match: ate
Depending on your operating system you might have to combine the \r and \n character escapes to create the correct new line sequence for your platform. For Microsoft Windows systems you should generally use \r\n which is a carriage return then line feed (CRLF). To simply match the end of a line or string use the dollar sign ($).

Match Grouping

Groups perform a few different functions. They allow the quantifiers (such as plus and star) to be applied to sections of the match instead of just individual characters.
A group is specified by the round brackets ( and ). If you want to match the round bracket characters you must use the escape character before the bracket e.g. \( or \).
This regex matches 'http://' optionally followed by 'www.' then starts a group and matches one or more of any character that is not a full stop/period (.) closes the group then matches '.com'.
Text:    http://www.yahoo.com/index.html and http://yahoo.com Regex:   http://(www\.)?([^\.]+)\.com  Matches:
  http://www.yahoo.com http://yahoo.com
The question mark after the group (www\.) applies to the whole group making it optional.

An example in C#

The regular expression classes are in the System.Text.RegularExpressions namespace.
using System.Text.RegularExpressions;
The Regex class represents a regular expression. A regular expression pattern must be specified when creating a Regex object. The pattern cannot be changed.

Regex exp = new Regex(     @"http://(www\.)?([^\.]+)\.com",     RegexOptions.IgnoreCase);  string 
InputText = "http://www.yahoo.com/";
The MatchCollection class stores a list of successful matches found by applying the regular expression pattern to an input string.

MatchCollection MatchList = exp.Matches(InputText); Match FirstMatch = MatchList[0];  Console.
WriteLine(FirstMatch.Value);
The Group class represents a group within the regex pattern. Each Match object has a Groups collection.
Group GroupCurrent; 
for (int i = 1; i < FirstMatch.Groups.Count; i++) 
{
     GroupCurrent = FirstMatch.Groups[i];
The Success property on the group can be used to check if the Group matched or not.
if (GroupCurrent.Success)
 {
         Console.WriteLine("\tMatched:" + GroupCurrent.Value);
  }
  else
  {
         Console.WriteLine("\tGroup didn't match");
   }
 }
Groups within a Match can be referenced by number or by name (see below).
if (MatchList.Count > 0)
 {
     if (MatchList[1].Success)
     {
         Console.WriteLine("Group 1 matched");
     }
 }
Matches also allow sections of the match to be used in replacement expressions when using Regex.Replace().

Named Groups

Groups can be named to allow easier identification with the following syntax.
(?<NameOfGroup>expression)

Matching boundaries between words

To match a boundary between a word character (\w) and a non-word character (\W) use \b. The match will occur at the first or last character in words separated by any nonalphanumeric characters. For example, the following Regular Expression matches one or more word characters followed by a word boundary followed by a hyphen (-) followed by another word boundary followed by one or more word characters.
Text:    Anna Jones and John William-Scott went to lunch- 
with an anaconda Regex:   \w+\b-\b\w+
Options: IgnoreCase Matches: Anna Jones and John William-Scott went to lunch- 
with an anaconda William-Scott
Use \B to specify that a match must not occur on a \b boundary.

Regular Expression Options

Regular Expression Options can be used in the constructor for the Regex class.
RegexOptions.None - Specifies that no options are set.
RegexOptions.IgnoreCase - Specifies case-insensitive matching.
RegexOptions.Multiline - Multiline mode. Changes the meaning of ^ and $ so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string.
RegexOptions.Singleline - Specifies single-line mode. Changes the meaning of the dot (.) so it matches every character (instead of every character except \n).
RegexOptions.ExplicitCapture - Specifies that the only valid captures are  groups that are explicitly named or in the form (?<name>...).
RegexOptions.IgnorePatternWhitespace - Eliminates unescaped white space from the pattern and enable s comments marked with the hash sign (#).
RegexOptions.Compiled - Specifies that the regular expression is compiled to an assembly. The regular expression will be faster to match but it takes more time to compile initially. This option (although tempting) should only be used when the expression will be used many times. e.g. in a foreach loop
RegexOptions.ECMAScript - Enables ECMAScript-compliant behavior for the expression. This flag can be used only in conjunction with the IgnoreCase, Multiline, and Compiled flags. The use of this flag with any other flags results in an exception.
RegexOptions.RightToLeft - Specifies that the search will be from right to left instead of from left to right.

regular expression in javascript

 
 

 

A regular expression object is an instance of the RegExp object. Each regular expression object consists of a
pattern that is used to locate matches within a string. Patterns for a regular expression can be simple strings or
significantly more powerful expressions that use a notation that is essentially a language unto itself. The
implementation of regular expressions in JavaScript 1.2 is very similar to the way they are implemented in Perl.
You can read more about these concepts in books covering JavaScript 1.2 or later.

 

To create a regular expression object, surround the pattern with forward slashes, and assign the whole
expression to a variable. For example, the following statement creates a regular expression with a pattern that
is a simple word:

var re = /greet/;
 

The re variable can then be used as a parameter in a variety of methods that search for the pattern within some
string (you may also use an expression directly as a method parameter, rather than assigning it to a variable).

 

Regular expression notation also consists of a number of metacharacters that stand in for sometimes complex
ideas, such as the boundary on either side of a word, any numeral, or one or more characters. For example,
to search for the pattern of characters shown above but only when the pattern is a word (and not part of a
word such as greetings), the regular expression notation uses the metacharacters to indicate that the pattern
includes word boundaries on both sides of the pattern:

var re = /\bgreet\b/;
 

The following table shows a summary of the regular expression notation used in JavaScript 1.2.

 

When you create a regular expression, you may optionally wire the expression to work globally (as you
probably do if the regular expression is doing a search-and-replace operation with a method, and your goal
is a "replace all" result) and to ignore case in its matches. The modifiers that turn on these switches are the
letters g and i. They may be used by themselves or together as gi.

 

Once you have established a pattern with the regular expression notation, all the action takes place in the
regular expression object methods and the String object methods that accept regular expression parameters.

 
Character Matches Example
\b Word boundary /\bto/ matches "tomorrow"/to\b/ matches "Soweto"
\B Word nonboundary /\Bto/ matches "stool" and "Soweto"/to\B/ matches "stool" and "tomorrow"
\d Numeral 0 through 9 /\d\d/ matches "42"
\D Nonnumeral /\D\D/ matches "to"
\s Single whitespace /under\sdog/ matches "under dog"
\S Single nonwhitespace /under\Sdog/ matches "under-dog"
\w Letter, numeral, or underscore /1\w/ matches "1A"
\W Not a letter, numeral, or underscore /1\W/ matches "1%"
. Any character except a newline /../ matches "Z3"
[...] Any one of the character set in brackets /J[aeiou]y/ matches "Joy"
[^...] Negated character set /J[^eiou]y/ matches "Jay"
* Zero or more times /\d*/ matches "", "5", or "444"
? Zero or one time /\d?/ matches "" or "5"
+ One or more times /\d+/ matches "5" or "444"
{n} Exactly n times /\d{2}/ matches "55"
{n,} n or more times /\d{2,}/ matches "555"
{n,m} At least n, at most m times /\d{2,4}/ matches "5555"
^ At beginning of a string or line /^Sally/ matches "Sally says..."
$ At end of a string or line /Sally.$/ matches "Hi, Sally."
 
Properties
 
constructor global ignoreCase lastIndex source
 
Methods
 
compile( ) exec( ) test( )
 
Creating a regular expression Object
 
var regExpressionObj = /pattern/ [g | i | gi]; var regExpressionObj = new RegExp(["pattern", ["g" | "i" | "gi"]]);
constructor NN 4 IE 4 ECMA 3 

Read/Write 

See this property for the Array object.

global, ignoreCase

 

 

Returns Boolean true if the regular expression object instance had the g or i modifiers (respectively) set when
it was created. If a regular expression object has both modifiers set (gi), you must still test for each property
individually.

 
Example
 
if (myRE.global && myRE.ignoreCase) {     ... }
 
Value

Boolean value: true | false.

lastIndex

 

This is the zero-based index value of the character within the string where the next search for the pattern begins.
In a new search, the value is zero. You can also set the value manually if you wish to start at a different location
or skip some characters.

 
Example
 
myRE.lastIndex = 30;
 
Value

Integer.

source
 

 

Returns a string version of the characters used to create the regular expression. The value does not include the
forward slash delimiters that surround the expression.

 
Example
 
var myREasString = myRE.source;
 
Value

String.

compile( )

compile("pattern"[, "g" | "i" | "gi"])

 

Compiles a regular expression pattern into a genuine regular expression object. This method is used primarily
to recompile a regular expression with a pattern that may change during the execution of a script.

 
Parameters
 
  • Any regular expression pattern as a quoted string. Modifiers for global, ignore case, or both must be
    supplied as a separate quoted parameter.
 
Returned Value

Reference to a regular expression instance.

exec( )

exec(string)

 

Performs a search through the string passed as a parameter for the current regular expression pattern. A typical
sequence follows the format:

var myRE = /somePattern/; var resultArray = myRE.exec("someString");
 

Properties of both the static RegExp and regular expression instance (myRE in the example) objects are updated
with information about the results of the search. In addition, the exec( ) method returns an array of data, much
of it similar to RegExp object properties. The returned array includes the following properties:

index

Zero-based index of starting character in the string that matches the pattern

input

The original string being searched

[0]

String of the characters matching the pattern

[1]...[n]

Strings of the results of the parenthesized component matches

 

You can stow away the results of the exec( ) method in a variable, whereas the RegExp property values change
with the next regular expression operation. If the regular expression is set for global searching, a subsequent call
to myRE.exec("someString") continues the search from the position of the previous match.

 

If no match is found for a given call to exec( ), it returns null.

 
Parameters
 
  • The string to be searched.
 
Returned Value

An array of match information if successful; null if there is no match.

test( )

test(string)

 

Returns Boolean true if there is a match of the regular expression anywhere in the string passed as a parameter,
false if not. No additional information is available about the results of the search. This is the fastest way to find out
if a string contains a match for a pattern.

 
Parameters
 
  • The string to be searched.
 
Returned Value

Boolean value: true | false.

Friday, March 11, 2011

How to get current date in Javascript

Here is simple program that will show you how to get current system date in Javascript and display
on the page.
Date()
this the function which used to get the current date in the javascript.
<!--
<html>
<head>
<script type="text/javascript">
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
</head>
<body>
<h1>My First Web Page</h1>
<p id="demo">This is a paragraph.</p>
<button type="button" onclick="displayDate()">Display Date</button>
</body>
</html>
-->