The Date object has methods for manipulating dates and times. JavaScript stores dates as the number of milliseconds since January 1, 1970. The sample below shows the different methods of creating date objects, all of which involve passing arguments to the Date() constructor.
A few things to note:
To create a Date object containing the current date and time, the Date() constructor takes no arguments.
When passing the date as a string to the Date() constructor, the time portion is optional. If it is not included, it defaults to 00:00:00. Also, other date formats are acceptable (e.g, "11/06/2009" and "11-06-2009").
When passing date parts to the Date() constructor, dd, hh, mm, ss, and ms are all optional. The default of each is 0.
Months are numbered from 0 (January) to 11 (December).
Some common date methods are shown below. In all the examples, the variable RIGHT_NOW contains "Fri Nov 06 00:23:54:650 EDT 2009".
A regular expression is a pattern that specifies a list of characters. In this section, we will look at how those characters are specified :
Start and End ( ^ $ )
A caret (^) at the beginning of a regular expression indicates that the string being searched must start with this pattern.
The pattern ^foo can be found in "food", but not in "barfood".
A dollar sign ($) at the end of a regular expression indicates that the string being searched must end with this pattern.
The pattern foo$ can be found in "curfoo", but not in "food".
Number of Occurrences ( ? + * {} )
The following symbols affect the number of occurrences of the preceding character: ?, +, *, and {}.
A questionmark (?) indicates that the preceding character should appear zero or one times in the pattern.
The pattern foo? can be found in "food" and "fod", but not "faod".
A plus sign (+) indicates that the preceding character should appear one or more times in the pattern.
The pattern fo+ can be found in "fod", "food" and "foood", but not "fd".
A asterisk (*) indicates that the preceding character should appear zero or more times in the pattern.
The pattern fo*d can be found in "fd", "fod" and "food".
Curly brackets with one parameter ( {n} ) indicate that the preceding character should appear exactly n times in the pattern.
The pattern fo{3}d can be found in "foood" , but not "food" or "fooood".
Curly brackets with two parameters ( {n1,n2} ) indicate that the preceding character should appear between n1 and n2 times in the pattern.
The pattern fo{2,4}d can be found in "food","foood" and "fooood", but not "fod" or "foooood".
Curly brackets with one parameter and an empty second paramenter ( {n,} ) indicate that the preceding character should appear at least n times in the pattern.
The pattern fo{2,}d can be found in "food" and "foooood", but not "fod".
Common Characters ( . \d \D \w \W \s \S )
A period ( . ) represents any character except a newline.
The pattern fo.d can be found in "food", "foad", "fo9d", and "fo*d".
Backslash-d ( \d ) represents any digit. It is the equivalent of [0-9].
The pattern fo\dd can be found in "fo1d", "fo4d" and "fo0d", but not in "food" or "fodd".
Backslash-D ( \D ) represents any character except a digit. It is the equivalent of [^0-9].
The pattern fo\Dd can be found in "food" and "foad", but not in "fo4d".
Backslash-w ( \w ) represents any word character (letters, digits, and the underscore (_) ).
The pattern fo\wd can be found in "food", "fo_d" and "fo4d", but not in "fo*d".
Backslash-W ( \W ) represents any character except a word character.
The pattern fo\Wd can be found in "fo*d", "fo@d" and "fo.d", but not in "food".
Backslash-s ( \s) represents any whitespace character (e.g, space, tab, newline, etc.).
The pattern fo\sd can be found in "fo d", but not in "food".
Backslash-S ( \S ) represents any character except a whitespace character.
The pattern fo\Sd can be found in "fo*d", "food" and "fo4d", but not in "fo d".
Grouping ( [] )
Square brackets ( [] ) are used to group options.
The pattern f[aeiou]d can be found in "fad" and "fed", but not in "food", "faed" or "fd".
The pattern f[aeiou]{2}d can be found in "faed" and "feod", but not in "fod", "fed" or "fd".
Negation ( ^ )
When used after the first character of the regular expression, the caret ( ^ ) is used for negation.
The pattern f[^aeiou]d can be found in "fqd" and "f4d", but not in "fad" or "fed".
Subpatterns ( () )
Parentheses ( () ) are used to capture subpatterns.
The pattern f(oo)?d can be found in "food" and "fd", but not in "fod".
Alternatives ( )
The pipe ( ) is used to create optional patterns.
The pattern foo$^bar can be found in "foo" and "bar", but not "foobar".
Escape Character ( \ )
The backslash ( \ ) is used to escape special characters.
The pattern fo\.d can be found in "fo.d", but not in "food" or "fo4d".
Backreferences
Backreferences are special wildcards that refer back to a subpattern within a pattern. They can be used to make sure that two subpatterns match. The first subpattern in a pattern is referenced as \1, the second is referenced as \2, and so on.
A more practical example has to do matching the delimiter in social security numbers. Examine the following regular expression.
^\d{3}([\- ]?)\d{2}([\- ]?)\d{4}$
Within the caret (^) and dollar sign ($), which are used to specify the beginning and end of the pattern, there are three sequences of digits, optionally separated by a hyphen or a space. This pattern will be matched in all of following strings (and more).
•123-45-6789
•123 45 6789
•123456789
•123-45 6789
•123 45-6789
•123-456789
Trim function, trims all leading and trailing spaces:
String.prototype.trim = function(){return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}
StartsWith to check if a string starts with a particular character sequecnce:
String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}
EndsWith to check if a string ends with a particular character sequecnce:
String.prototype.endsWith = function(str) {return (this.match(str+"$")==str)}
All these functions once loaded will behave as built-in JavaScript functions. Here are few examples:
var myStr = “ Earth is a beautiful planet ”;
var myStr2 = myStr.trim(); //==“Earth is a beautiful planet”;
if (myStr2.startsWith(“Earth”)) // returns TRUE
if (myStr2.endsWith(“planet”)) // returns TRUE
if (myStr.startsWith(“Earth”)) // returns FALSE due to the leading spaces…
if (myStr.endsWith(“planet”)) // returns FALSE due to trailing spaces…
String Properties and Methods :
In JavaScript, there are two types of string data types: primitive strings and String objects. String objects have many methods for manipulating and parsing strings of text. Because these methods are available to primitive strings as well, in practice, there is no need to differentiate between the two types of strings.
Some common string properties and methods are shown below.
In all the examples, the variable MY_STRING contains "Webucator".
Create an aspx button :
asp:button text="Click Me" runat="server"
In the server side add the button client side functions.
btnOpen.Attributes.Add("onClick", "return false;");
btnOpen.Attributes.Add("onMouseOver", "mousemoveover();");
Javascript function to move the button
function mousemoveover()
{
var btn = document.getElementById("btnOpen");
var sHeight = screen.height - 400;
var sWidth = screen.width - 100;
btn.style.marginLeft = Math.floor(Math.random()*sWidth) + 'px';
btn.style.marginTop = Math.floor(Math.random()*sHeight) + 'px';
}
//To Check the given control contain only Alpha numeric values
function CheckKeyIsAlphanumerics(oControl)
{
if (/\W/.test(oControl.value))
{
alert("Please enter alphanumerics only");
oControl.value ="";
return false;
}
}
// Another way to validate this is
function CheckKeyIsAlphanumerics(oControl)
{
var regexNum = /\d/;
var regexLetter = /[a-zA-z]/;
if(!regexNum.test(oControl.value) !regexLetter.test(oControl.value))
{
alert('Type alphanumeric character');
return false;
}
}
//To Check the given control contain only Numeric Values
function CheckKeyIsNumerics(oControl)
{
if (/[^0-9]/.test(oControl.value))
{
alert("Please enter only numerics.");
oControl.value ="";
return false;
}
}
To Trim the Empty space on both side
String.prototype.trim = function() {return this.replace(/^\s+\s+$/g,"");}
//Checking Blank Values
if (document.getElementById("txtName").value.trim()=="")
{
alert("Name Field can not be blank");
document.getElementById("txtName").focus();
return false;
}
//Checking AlphaNumeric Values
if (CheckKeyIsAlphanumerics(document.getElementById(txtName)))
return false;
else
return true;
function CheckKeyIsAlphanumerics(str)
{
var string = str.value.trim();
var iChars = "01234546789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (var i = 0; i lt; string.length; i++)
{
if (iChars.indexOf(string.charAt(i)) == -1)
return true;
}
return false;
}
To allow special characters in condition means add the special character in the iChars Variable.
//EmailValidation in the Textbox
if (isValidEMailID(document.getElementById("txtEmail")))
return true;
else
return false;
function isValidEMailID(oControl)
{
if(oControl.value != "")
{
if((/([A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}][A-Za-z]\w*(\.[A-Za-z]\w*)+)$/.test(oControl.value))==false)
{
alert("Invalid Email Format.");
oControl.value ="";
return false;
}
}
return true;
}
Pass the Control ID as Parameter for the Function.
Pop-up New Window from Java Script
window.open('Form2.aspx?id=1,value=raja','page1','height=350,width=470,location=no,menubar=no,
resizable=no,scrollbars=yes,toolbar=no');
Form2.aspx - Name of the aspx page to redirectValues can be passed to next page thro query stringCustom size for Height and WidthBased on the need give the menubar, resizable, scrollbars, toolbar display
Changing the Color of the Button in a page
function BtnColorChange()
{
var f = document.getElementById("form1");
var inputs = f.getElementsByTagName("input");
for(var i = 0; i < inputs.length; i++)
{
if(inputs[i].type == "submit")
inputs[i].style.color = '#C9C5C5';
}
}
JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer, Firefox, Chrome, Opera, and Safari.
What is JavaScript?
* JavaScript was designed to add interactivity to HTML pages
* JavaScript is a scripting language
* A scripting language is a lightweight programming language
* JavaScript is usually embedded directly into HTML pages
* JavaScript is an interpreted language (means that scripts execute without preliminary compilation)
* Everyone can use JavaScript without purchasing a license
What can a JavaScript do?
JavaScript gives HTML designers a programming tool - HTML authors are normally not programmers, but JavaScript is a scripting language with a very simple syntax! Almost anyone can put small "snippets" of code into their HTML pages
JavaScript can put dynamic text into an HTML page - A JavaScript statement like this: document.write(" + name + ") can write a variable text into an HTML page
JavaScript can react to events - A JavaScript can be set to execute when something happens, like when a page has finished loading or when a user clicks on an HTML element
JavaScript can read and write HTML elements - A JavaScript can read and change the content of an HTML element
JavaScript can be used to validate data - A JavaScript can be used to validate form data before it is submitted to a server. This saves the server from extra processing
JavaScript can be used to detect the visitor's browser - A JavaScript can be used to detect the visitor's browser, and - depending on the browser - load another page specifically designed for that browser
JavaScript can be used to create cookies - A JavaScript can be used to store and retrieve information on the visitor's computer