Archive for April, 2009

Disable Text Selection in a web page

Here is a sample code snippet, which can be used to disable the text selection in a web page.

But selecting text inside input fields, such as Textarea, textbox and Select (combo) box is not restricted.

//get the browser name,version details
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }

//variables to hold the browser names, and versions
var is_ie = false;
var is_ff15 = false;
var is_ff20 = false;
var is_ff30 = false;
    is_ie = true;
else if(agent('firefox/3.0.0'))
    is_ff30 = true;
else if(agent('firefox/2.0.0'))
    is_ff20 = true;
else if(agent('firefox/1.5'))
    is_ff20 = true;

/* This is the function which Disables Text Selection in the Document*/
function selstart(e){
    //if not ie and !not ie then return
    if (!agent('msie') && !(!agent('msie')))

    var selobj=(!agent('msie'))? : event.srcElement
    var topelement=(!agent('msie'))? "HTML" : "BODY"

        if ( selobj.tagName!=topelement &&
                (selobj.tagName=='SELECT' ||
                selobj.tagName=='TEXTAREA' ||
                  return true;
            return false;


This function uses another function named agent to get the browser agent name, whether the user’s browser is Firefox or IE or others. And some global variables for storing browser names and versions.

Whenever a select text event (onselectstart) is fired, the function selstart is executed and the text selection inside the web page is restricted. The function will return true to disable text selection.

Javascript function to Validate IP Address (IPv4)

In one of my current project ( Front end for an ISM Device ), I’ve to validate IP Addresses using JavaScript.

And wrote this function fnValidateIPAddress

The function takes an IP Address as input string and returns either true if the input string is a valid IP Address or false if not. This function checks for the Class C IP Addresses only. This function can be changed further to validate different IP Classes.

Read the rest of this entry »

Feedback Form