js not working consistently

Discussion in 'Javascript' started by Peter, Jan 20, 2008.

  1. Peter

    Peter Guest

    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>
     
    Peter, Jan 20, 2008
    #1
    1. Advertising

  2. Peter

    pr Guest

    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_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.
     
    pr, Jan 20, 2008
    #2
    1. Advertising

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

    >> }
    >> 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$>
     
    Thomas 'PointedEars' Lahn, Jan 20, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    542
  2. Replies:
    1
    Views:
    402
    Thangavel M
    Oct 23, 2005
  3. syockit
    Replies:
    2
    Views:
    281
    Dave Angel
    Jul 2, 2010
  4. Jarmo Pertman
    Replies:
    3
    Views:
    164
    Jarmo Pertman
    Nov 27, 2010
  5. Replies:
    4
    Views:
    132
Loading...

Share This Page