Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > js not working consistently

Reply
Thread Tools

js not working consistently

 
 
Peter
Guest
Posts: n/a
 
      01-20-2008
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>
 
Reply With Quote
 
 
 
 
pr
Guest
Posts: n/a
 
      01-20-2008
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.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-20-2008
pr wrote:
> 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'?


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.

>> function checkDecimal(obj, objStr){
>> var objNumber;
>> objNumber = ' ';

>
> That's a terrible name for a string variable, but if it works for you...


var objNumber = ' ';

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


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.

>> }
>> 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");


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
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pool Module: iterator does not yield consistently with differentchunksizes syockit Python 2 07-02-2010 11:55 AM
PC does not consistently hibernate =?Utf-8?B?RGVuemlsIEZlcm5hbmRlcw==?= Windows 64bit 7 07-21-2006 07:44 AM
Clipboard? Cut & Paste not working consistently... DWACON Computer Support 1 03-03-2006 11:33 AM
Page_Load not firing first consistently mollyf@hotmail.com ASP .Net 2 05-31-2005 01:58 PM
XP system tray program not showing up consistently Cloud Burst Computer Support 6 06-16-2004 03:46 PM



Advertisments