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

How to encode and decode URL strings?

1802


How to block double clicks

1626


sample code to auto focusing the first field in a form

2036


write a code to generate pseudorandom numbes

1793


program to show a progress bar

2132






code to keep a page Out of the browser history

1663


how to convert between arrays and strings

2117


how to Scroll a DIV content

2165


how to Auto Scroll the page

1636


code to set the main window's size

1711


create Drop-Down Navigation Menus

1708


maximizing the main window

1643


how to pass data between pages using Cookies

1856


Create a ViewState Property?

2294


code to get the coordinates of a Click Event

1748