Tue, 14 Mar 2006

Formating Numbers in a Text Box

I recently needed to control the format that was being entered into a text box on a form. I needed the user to enter a currency amount, however, I did not want dollar signs, commas or decimal places to be entered. I simply wanted the raw number.

This is one way to do it.

<html>
<head>
<title>Number Format</title>

<script language="JavaScript">
function formatNumber(e, field) {
  key=e.keyCode;
  // Allow use of return, left and right cursor keys
  if ((key==13)||(key==37)||(key==39)) return;
  temp=field.value.replace(/[^0-9]|^0+/g,"");
  field.value=temp;
}
</script>

</head>

<body>
 <form name="form1" method="post" action="">
   Enter Salary:
     <input type="text" name="amount" id="amount"
onKeyUp="formatNumber(event, this)">
 </form>
</body>
</html>

The script is called on the "onKeyUp" event of the input field and strips out any character that is not a number.



posted: 00:38 | 0 comments | tags: ,


Comments

Name:


E-mail:


URL:


Comment:


© 2008 PlatosCave.net