Peter wrote:
> I have the following code being called by a form. As soon as I enter
> a Rent such as asdff to test it, the next time I enter a number it
> doesn't format and if I enter text it just becomes blank regardless of
> whether its FOR SALE. The only way to get the form working again is
In which browser(s)? I don't see any obvious errors in what you've
supplied in Firefox, Opera, Konqueror or IE6.
> to either delete the temporary files or close and reopen the browser
Which temporary files? Do you mean 'empty the browser cache'?
> window. Does anyone see what might be my problem.
>
> form
> -----
> <input type="text" name="rent" size="32" value=""
> onBlur="checkDecimal(this,this.value)">
Simpler to use just checkDecimal(this) and obj.value in your function.
Also use onchange instead of onblur.
>
> after header
Hopefully you mean inside either the <head> or <body> element. You will
get more accurate answers to your query if you provide a URL or the
source of a whole (but small) example page.
> ---------------
> <script type="text/javascript">
> function checkDecimal(obj, objStr){
> var objNumber;
> objNumber = ' ';
That's a terrible name for a string variable, but if it works for you...
> if(isNaN(objStr) && objStr!=''){
isNaN('') == false, so you can omit the "&& objStr!=''"
> if (objStr == 'FOR SALE') objNumber = 'FOR SALE';
> if (objStr == 'for sale') objNumber = 'FOR SALE';
> if (objStr == 'SALE') objNumber = 'FOR SALE';
> if (objStr == 'sale') objNumber = 'FOR SALE';
> if (objStr == 'FOR LEASE') objNumber = 'FOR LEASE';
> if (objStr == 'for lease') objNumber = 'FOR LEASE';
> if (objStr == 'LEASE') objNumber = 'FOR LEASE';
> if (objStr == 'lease') objNumber = 'FOR LEASE';
> if ((objNumber != 'FOR SALE') && (objNumber != 'FOR LEASE'))
> objNumber = ' ';
>
It works, but you can make it a lot simpler with a RegExp. <URL:
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Working_with_Regular_Exp ressions>
Something like
if (/^sale$|^for sale$/i.test(objStr)) ...
would seem to be in order.
>
>
> }
> else if(objStr==''){
>
> objNumber = '0.00';
> }
> else if(objStr.indexOf('.')!=-1){
> if(((objStr.length) - (objStr.indexOf('.')))>3){
> objStr = objStr.substr(0,((objStr.indexOf('.'))+3));
> }
Again, you could use
objStr = objStr.replace(/(\.\d\d)\d+$/, "$1");
That's if you don't care about rounding. Otherwise
objStr = "" + (Math.round(100 * objStr)/100);
> if(objStr.indexOf('.')==0){
> objStr = '0' + objStr;
> }
> var sLen = objStr.length;
> var TChar = objStr.substr(sLen-3,3);
> if(TChar.indexOf('.')==0){
> objNumber = objStr;
> }
> else if(TChar.indexOf('.')==1){
> objNumber = objStr + '0';
> }
> else if(TChar.indexOf('.')==2){
> objNumber = objStr + '00';
> }
objStr = objStr.replace(/\..*$/, function(a) {
return a + ".00".substring(a.length);
});
> }
> else{
> objNumber = objStr + '.00';
> }
> obj.value = objNumber;
> }
>
> </script>
Not all of these comments will be relevant to your immediate problem. If
it persists after you rectify any errors shown by the HTML validator at
<URL: http://validator.w3.org/>, please supply the URL or source of a
minimal non-working example and identify the browser(s) it fails on.