js not working consistently

P

Peter

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
to either delete the temporary files or close and reopen the browser
window. Does anyone see what might be my problem.

tia,
pete

form
-----
<input type="text" name="rent" size="32" value=""
onBlur="checkDecimal(this,this.value)">

after header
---------------
<script type="text/javascript">
function checkDecimal(obj, objStr){
var objNumber;
objNumber = ' ';
if(isNaN(objStr) && 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 = ' ';



}
else if(objStr==''){

objNumber = '0.00';
}
else if(objStr.indexOf('.')!=-1){
if(((objStr.length) - (objStr.indexOf('.')))>3){
objStr = objStr.substr(0,((objStr.indexOf('.'))+3));
}
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';
}
}
else{
objNumber = objStr + '.00';
}
obj.value = objNumber;
}

</script>
 
P

pr

Peter said:
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

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_Expressions>

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.
 
T

Thomas 'PointedEars' Lahn

pr said:
In which browser(s)? I don't see any obvious errors in what you've
supplied in Firefox, Opera, Konqueror or IE6.


Which temporary files? Do you mean 'empty the browser cache'?

Probably yes. In Internet Explorer the "Temporary Internet Files" (TIF)
folder has always served as the browser cache. Since you have tested with
IE6, you should know that.
That's a terrible name for a string variable, but if it works for you... :)

var 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_Expressions>

Something like

if (/^sale$|^for sale$/i.test(objStr)) ...

would seem to be in order.

if (/^(sale|for sale)$/i.test(objStr)) ...

and eventually

if (/^(for )?sale$/i.test(objStr)) ...

contains less redundancy. And in this case,

if (/sale/i.test(objStr)) ...

may suffice.
Again, you could use

objStr = objStr.replace(/(\.\d\d)\d+$/, "$1");

It does not matter what follows after the second digit, but it does matter
what precedes the dot.

objStr = objStr.match(/\d+\.\d\d/)[0];
[...]
That's if you don't care about rounding. Otherwise

objStr = "" + (Math.round(100 * objStr)/100);

Please consult the FAQ about why this approach is a bad idea and viable
alternatives to it.

http://jibbering.com/faq/


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top