31
Mar 08

Javascript isNaN()

The isNaN() function is used to check if the data contained in the Javascript variable is a number or not. The function returns true if the data is not a number and false if the data is a number.

This function is of great help while performing javascript validation to check if a user has entered only numeric values into the textboxes as specified by you.

Here is an example:

var a = “hi”;

var b = “123″;

if(isNaN(a))

{

alert(“Variable ‘a’ is not a number.”);

}

if(isNaN(b))

{

alert(“Variable ‘b’ is not a number.”);

}

The output will be “Variable ‘a’ is not a number.”

A note to remember: “isNaN() ” will return false for “1e1″ .

I mean, if you have numeric data with the letter ‘e’ in it, then the data is still considered to be numeric!

why is that?

Well, cause it treats the value to be exponential. So if you plan to validate for a numeric field and do not want to allow the letter ‘e’ then remember to validate it separately.

Well, as far as validating ‘e’ is considered… I wonder how you do that in Javascript!!

I mean we need to have a function which will tell us if a particular letter exists in a given data. Hmmmm… Donno if there exists a function like that in javascript. My next post will be on this :)


28
Mar 08

How to perform mathematical operations in javascript?

You might come across a situation wherein you need to perform some mathematical operations on the data entered by the user. Be it addition, subtraction , multiplication or division, you can do these calculations using Javascript easily.

Since all the variables in JavaScript are declared using ‘var’ as a datatype, we need to use a function to tell Javascript to treat the contents of the variable as a number on which calculations have to be performed.

All you need to do is to use the ‘Number()’ function.

The example below will make things clear for you :

var a = “5″;

var b = “5″;

var result = a + b;

alert(“The result is “+result); // This will give you an output of 55 and not 10.

var calculatedResult = Number(a) + Number(b);

alert(“The mathematical result is “+calculatedResult); //This will give the result as 10.


14
Mar 08

Disable right click(Context Menu) in your HTML page

This is going to be a small post. There’s not much to talk about.

HTML has an event called “oncontextmenu”. This event handles the right click event.

So, if you want to disable right click for any field in your page, all you have to do is add “oncontextmenu” inside the particular tag. The example below will make things clear.

Ex. Suppose you want to prevent users from copy pasting something into one of your text boxes. Then you need to write the input tag as follows:

<input type=”text” oncontextmenu=”return false;”>

That’s it!! The context menu does not appear because we are handling the right click event by returning false.

Alternatively, you can disable right click for the whole page as follows:

Just copy paste the following code after the end of your <form> tag.

<script>

document.oncontextmenu = function(){return false}

</script>