Friday, August 21, 2009

Allow to enter only numbers in textbox

At times there is a need when we want to allow users to enter only number.
Say when its phone number, instead of validation after submit we can restrict users to enter non numeric characters using the following script:

function isNumberKey(evt)
var charCode = (evt.which) ? evt.which : event.keyCode;
if(!((charCode>=48&&charCode<=57)|| (charCode==46) || (charCode==8))) return false; return true; } To use it in HTML: <input type="text" onkeypress="return isNumberKey(event);" />

Friday, August 14, 2009

Parse pop up blocker

All web browsers these days incorporate a popup blocker. These blockers are intended to stop popup windows from appearing except when they are requested by the person using the browser.

This means that a popup blocker is supposed to stop popup windows attached to such events as onload and onunload (and other events not directly associated with the person requesting a popup) from appearing but is not supposed to block those attached to an onclick event on a link (and other events that can be taken to mean that the person has requested it).

Unfortunately not all blockers handle this correctly. Sure they block any popups that are not requested but they also in many cases block requested popups. This is due to the way in which the browsers determine whether a popup was actually requested.

Not all browsers are smart enough to realize that if the onclick event on a link calls a function and the purpose of that function is to open a popup window that the popup has in fact been requested. They see the popup code in the function but are not clever enough to see that this code is called as a result of an action by the person using the browser. Coding our popup that way leads to the popup being blocked despite the fact that it was requested, a sure way to get someone annoyed that your site doesn't work properly despite the fact that the problem is actually their browser.

The way to fix this for many of these dumb browsers is to place the popup code into the onclick itself instead of calling a function. This can result in rather messy HTML but makes sure that the browser has the best possible chance to see that the popup is only created in response to a definite action and does not occur automatically.

For example, a Javascript generated popup could be coded like this:

<a href="#" onclick="popWin ='','name','height=255,width=250, toolbar=no,directories=no,status=no,menubar=no, scrollbars=no,resizable=no'); openPopup(popWin);">popup</a>

The displayPopup function would create the content for the popup window like this:

function openPopup(TheNewWin) {

TheNewWin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:\/\/\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd"><html xmlns="http:\/\/\/1999\/xhtml">');

TheNewWin.document.write('<head><title>Popup<\/title><\/head><body style="overflow:hidden" bgcolor="#ffffff"> <p>This is an example of a popup window.<\/p>');

TheNewWin.document.write('<p>Provided that your web browser supports Javascript then this window should be 250 pixels wide and 255 pixels high, there should not');

TheNewWin.document.write(' be any toolbars etc. and the window cannot be resized.<\/p><hr \/> <p align="right"><a href="#" onclick="self.close();return false;">Close');

TheNewWin.document.write(' Window<\/a><\/p> <\/body><\/html>');


I can't guarantee that your popup will appear if you code it this way. All I can guarantee is that your popup will have a better chance of making it past the blockers if coded like this as it makes it more obvious to the popup blockers that the popup window has actually been requested by the person viewing the page and is not being generated automatically.

Courtesy: Stephen Chapman