[Javascript] text box

Rodney Myers rodney at aflyingstart.net
Sun Jul 14 04:43:14 CDT 2002


I don't think there is any instant solution, though you may find scripts out
there that are helpful.

The principle would be to take input and using the onChange or onBlur event
pass the input object to a function. The function can then work on the
appropriateness of the input and pass a cleaned up value back.

<input name="myInput" onChange="cleanUp(this)">

Try this example

<html><head><title>Money Input</title>
<script>

function moneyFormat(V){
var intPart,decPart
ret=V*100;ret=Math.round(ret);
ret=V<.1?"0"+ret:ret;
ret=V< 1?"0"+ret:""+ret;
intPart=ret.substring(0,ret.length-2);
decPart=ret.substring(ret.length-2);
ret=intPart+"."+decPart;
return(ret);
}

function process(el){
el.value=moneyFormat(decimals(el.value));
}

function decimals(str){ return(clean(str,"1234567890.")) }

function clean(str,permitted){
str=""+str;
var nchar,n;var out="";
for(n=0;n<str.length;n++)
 {
 nchar=str.charAt(n);
 out+=permitted.indexOf(nchar)>-1?nchar:"";
 }
return(out);
}



</script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<p>&nbsp;</p>
<form onSubmit="return(false);">
<table width="40%" border="1" cellspacing="0" cellpadding="5" align="center">
  <tr>
    <td>Input amount</td> <td><input type="text" name="anount" size="10"
onChange="process(this)"> </td>
  </tr>

</table>
</form>
</body></html>

hth

Rodney

Andrew Dunn wrote:

> Hi,
>         Is it possible to format a text box "<input type=text>" so it only
> accepts certain values like a date value or a currency value?
>
> Thanks.





More information about the Javascript mailing list