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: ,


Mon, 13 Mar 2006

URL Rewriting in JavaScript

At work I am constantly flipping between a production and a development web server. The live site would have a URL like, "www.mysite.com" and the development site one like, "dev.mysite.com". I was getting tired of removing the "www" from the address and replacing it with "dev" and back again for all the different URLs. This script is a result of that. By simply clicking a saved bookmark on my browser toolbar I can automatically switch back and forth between the production and development servers with ease.

Right-click on the following link, DevSwitch, and select either "Bookmark This Link..." in FireFox or "Add to Favorites..." in Internet Explorer.

The Bookmarklet code is outlined below.

javascript:(function(){
    x=location.href;
    y=(x.indexOf('www')!=-1)?x.replace('www','dev'):x.replace('dev','www');
    location.href=y;
})()


posted: 23:58 | 0 comments | tags: , ,


© 2008 PlatosCave.net