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?
Yes, I was looking at the examples here: http://stackoverflow.com/questions/1133770/how-do-i-convert-a-string-into-an-integer-in-javascript but it just doesn't make much sense to me at the moment.
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
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?
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.
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): This would be the tests, hope it covers everything. Cheers.
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.
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' ?