validation code / function to allow only NUmbers in a text box

Answer Posted / kevin konkle

//Have this be called off the keypress event of your
textbox control.
function numberFormat(fld, e)
{
var key = '';

//This string contains all characters you want to allow
var strCheck = '0123456789';
var whichCode = (window.Event) ? e.which : e.keyCode;

if (whichCode == 13) return true; // Enter
if (whichCode == 8) return true; // Delete

// Get key value from key code
key = String.fromCharCode(whichCode);

// Not a valid key
if (strCheck.indexOf(key) == -1) return false;

//If it is a legal value then add it to the text box
fld.value += key;

return false;
}

Is This Answer Correct ?    6 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Create a Menu that can be activated while clicking on Right Mouse button

1707


advance the focus to next consecutive fields when Enter Key is pressed

1794


How to encode and decode URL strings?

1798


communication between main window and new windows

1701


write a code to generate pseudorandom numbes

1786






how to create a Draggable element

1968


create Drop-Down Navigation Menus

1705


function to combine two or more arrays

1702


how to copy form data between different pages

1678


code to keep a page Out of the browser history

1653


code to create a new window

1754


How to block double clicks

1620


how to pass data between pages using Frames

2147


code to set the main window's size

1708


how to pass data between pages using Cookies

1850