How to allow numbers, dot and comma in a text box in Javascript?

Here is the function:

 

function valid(field) {
var re = /\d*\.\,/;
if (!re.test(field.value)) {
alert(“Only numbers, dot and comma are allowed!”);
}

}

How to remove an element from an array using its index?

If you want to remove an element from an array using its index, then you simply use splice() method

If you want to remove an element from an array using its index, then you simply use splice() method.

This method can be used to add and remove the element from the array in javascript.

Have a look at the below function :

function removeFromArrayByIndex(arr, index) {

arr.splice(index, 1);

}

test = new Array();

test[0] = ‘onion’;

test[1] = ‘tomato’;

test[2] = ‘beans’;

test[3] = ‘peas’;

//alert(“Array before removing elements: “+test);

removeByIndex(test, 2);

//alert(“Array after removing elements: “+test);

Check the results by the alerts.

How to convert javascript array to CSV?

It is very simple to convert an array to CSV.

Suppose you have a JS array of strings like this:

var vegetables= [‘onion’, ‘ladyfinger’, ‘peas’];
var str = vegetables.valueOf();

The valueOf() method will convert an array in javascript to a comma separated string.

Output:

onion, ladyfinger, peas

Javascript Back Button

Just like your browser’s back button, the following script allows the user to press a button to returns the user to the previous page.

Paste the following code where you want the button to appear on your page: 

<FORM> <INPUT TYPE=”Button” VALUE=”Previous Page” onClick=”history.go(-1)”> </FORM>

If you would rather use a link, paste the following code where you want the link to appear: 

<a href=”javascript:history.back(1)”>Previous Page</a>

Javascript Function to disable Enter Key

function disableEnterKey(e) {
var key;

if (window.event)
key = window.event.keyCode;     //IE
else
key = e.which;     //firefox

if (key == 32)
return false;
else
return true;
}