1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Stopping all instances of 0

Discussion in 'JavaScript' started by qwikad.com, Sep 11, 2016.

  1. #1
    So this stops if someone enters a 0, but it won't stop 00 or 0.00 or 0,00 or $0 etc. etc.

    form.elements['price'].value == '0'

    How do I stop any price that is a 0 no matter how that price is entered? Is it even possible with just the form.elements?
     
    qwikad.com, Sep 11, 2016 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #2
    turn it from a string into a number?
     
    sarahk, Sep 11, 2016 IP
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #3
    qwikad.com, Sep 11, 2016 IP
  4. NaughtySpider

    NaughtySpider Peon

    Messages:
    10
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #4
    Use parseFloat function. It will return float for numbers and NaN for other values, so both values will be "falsy".

    Example:

    if (!parseInt(form.elements['price'].value) {
        console.log('Nope! Use positive number!');
    }
    Code (JavaScript):
    But values like $1 will be converted to NaN and 1,1 will be converted to 1. So if you want smart system, you will have to use regular expressions.

    Read more here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseFloat
     
    NaughtySpider, Sep 11, 2016 IP
  5. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #5
    I tried using !parseInt(form.elements['price'].value) and it seems to be working fine, except it's also stopping decimals (0.05, 0.15 etc.). I want those to go through. Any way to fix that?
     
    qwikad.com, Sep 11, 2016 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,494
    Likes Received:
    4,457
    Best Answers:
    123
    Trophy Points:
    665
    #6
    multiply by 100 and then test?
     
    sarahk, Sep 11, 2016 IP
  7. NaughtySpider

    NaughtySpider Peon

    Messages:
    10
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    3
    #7
    I think simpler way will be to use HTML5 number type input:

    <input type="number" name="price" step="any">
    Code (markup):
    This input will only allow numeric values. Also don't forget to check for negative and very small values. Everything less than 0.01 is not valid price.
     
    NaughtySpider, Sep 12, 2016 IP
  8. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    160
    #8
    Hei :)

    This is not the case as the guy could be dealing with $0.00 for example, or maybe just a comma.

    Since this derp JS does not know type casting it's an overkill to cast it to a number first and then do whatever checks.
    So, my three-step idea is to do as follows:

    
    /* Step 1, convert to string */
    var price = price + '';
    
    /* Step 2, remove non-numeric characters and keep dots or commas for floats */
    var price = price.replace( /[^\d.,]/g, '' );
    
    /* Step 3, convert to float, in case you deal with float numbers */
    var price = parseFloat( price );
    
    if ( !isNaN( price ) && price == 0 )
        return false; // stop here, 0 detected;
    
    Code (JavaScript):
    Here's a test I've put together, you can play with it.
    
    console.group( 'zero-price detection' );
    
    var prices = [ '$12', '00', '0.17', '0,00', 0, 5, '$0' ];
    
    for ( var i = 0; i < prices.length; i++ )
    {
        /* Step 1, convert to string */
        var price = prices[i] + '';
    
        /* Step 2, remove non-numeric characters and keep dot for floats */
        var price = price.replace( /[^\d.,]/g, '' );
    
        /* Step 3, convert to float, in case you deal with float numbers */
        var price = parseFloat( price );
    
        if ( !isNaN( price ) && price == 0 )
            console.log( prices[i] + ' == 0' + '%c STOP, 0 DETECTED', 'color: red; font-weight: bold;' );
        else
            console.log( prices[i] + ' != 0' + '%c OK, CONTINUE', 'color: green; font-weight: bold;' );
    }
    
    console.groupEnd();
    
    Code (JavaScript):
    [​IMG]

    This would be the tests, hope it covers everything.
    Cheers.
     
    Last edited: Oct 9, 2016
    edduvs, Oct 9, 2016 IP
  9. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    160
    #9
    So, did my previous comment solve your problems or you're still having difficulties ?
     
    edduvs, Oct 21, 2016 IP
  10. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,151
    Likes Received:
    1,656
    Best Answers:
    29
    Trophy Points:
    475
    #10
    Thank you for the follow up. As it often happens I decided to simplify the whole thing. Instead of stopping all the instances of zero using javascript I decided to let them go through and instead not to show the price if it's not greater than 0 with a php code. Seems to be working fine.
     
    qwikad.com, Oct 21, 2016 IP
  11. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #11
    Why did nobody even bother trying val*1 to get the numeric representation?
     
    Einheijar, Oct 22, 2016 IP
  12. edduvs

    edduvs Well-Known Member

    Messages:
    394
    Likes Received:
    31
    Best Answers:
    3
    Trophy Points:
    160
    #12
    Because that's exactly what he did not want achieve. Why would anyone ever bother multiplying '$0.00' by 1. Makes no sense and it will result in a NaN.

    If, for example his price was '$1.13' which is a valid case, how would you multiply that by 1 and see if it's an instance of '0', '0.00', '$0.00', '00' ?
     
    edduvs, Oct 22, 2016 IP