1) Email Validation
if(document.getElementById('email').value!=""){
var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var address = document.getElementById('email').value;
if(reg.test(address) == false) {
alert('Invalid Email Address');
return false;
}
}
Above will validate for email address
2) URL Validation in JavaScript
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
var theurl=linkurl.value;
if(!(regexp.test(theurl))){
alert('Invalid Url');
document.getElementById('linkurl').focus();
return false;
}
Above will validate URL link with regular expression. these validations are used to validate a form data at the time of post.
regexp.test , here test is javascript function to test the presence of characters of regexp in string theurl
3) Get Value of URL in javascript
<script>
function getUrlParam(param)
{
param = param.replace(/([\[\](){}*?+^$.\\|])/g, "\\$1");
var regex = new RegExp("[?&]" + param + "=([^&#]*)");
var url = decodeURIComponent(window.location.href);
var match = regex.exec(url);
return match ? match[1] : "";
}
var param = getUrlParam("page");
alert(param);
//url can be http://www.mysite.com?page=3
</script>
4) Using Window.open method
The syntax of the window.open method is given below:
open (URL, windowName[, windowFeatures])
URL
The URL of the page to open in the new window. This argument could be blank.
windowName
A name to be given to the new window. The name can be used to refer this window again.
windowFeatures
A string that determines the various window features to be included in the popup window (like status bar, address bar etc)
The following code opens a new browser window with standard features.
window.open (
"http://www.javascript-coder.com"
,
"mywindow"
);
You can control the features of the popup using the last argument to the window.open method. The following code opens a window with a status bar and no extra features.
window.open ( "http://www.javascript-coder.com" , "mywindow" , "status=1" ); |
window.open ( "http://www.javascript-coder.com" , "mywindow" , "status=1,toolbar=1" ); |
The table shows the features and the string tokens you can use:
status | The status bar at the bottom of the window. |
toolbar | The standard browser toolbar, with buttons such as Back and Forward. |
location | The Location entry field where you enter the URL. |
menubar | The menu bar of the window |
directories | The standard browser directory buttons, such as What’s New and What’s Cool |
resizable | Allow/Disallow the user to resize the window. |
scrollbars | Enable the scrollbars if the document is bigger than the window |
height | Specifies the height of the window in pixels. (example: height=’350′) |
width | Specifies the width of the window in pixels. |
Examples..
The following code opens a window with menu bar. The window is re-sizable and is having 350 pixels width and 250 pixels height.
window.open (
"http://www.javascript-coder.com"
,
"mywindow"
,
"menubar=1,resizable=1,width=350,height=250"
);
moving window to desire location
You can use the window.moveTo function to move the popup window to a desired location.
The code below shows positioning the popup at a desired location.
function mypopup() |
{ |
mywindow = window.open( "http://www.javascript-coder.com" , "mywindow" , "location=1,status=1,scrollbars=1, width=100,height=100" ); |
mywindow.moveTo(0, 0); |
} |
PopUp On Exit
<
html
>
< head > |
< title >JavaScript Popup Example 3</ title > |
</ head > |
< script type = "text/javascript" > |
function exitpop() |
{ |
my_window = window.open("", "mywindow1", "status=1,width=350,height=150"); |
my_window.document.write('< h1 >Popup Test!</ h1 >'); |
} |
</ script > |
< body onunload = "javascript: exitpop()" > |
< h1 >JavaScript Popup Example 4</ h1 > |
</ body > |
</
html
>