0

Im trying to get an alert when adding wrong parameters on items in simplecart. When the field is empty or if its lower then 400 of if it's higher then 500.

everything works, but not the alert when field is empty.

Html:

<input required max="500" min="400" type="number"  class="item_width">

JavaScript:

<script>
simpleCart.bind( 'beforeAdd' , function( item ){
if( item.get( 'width' ) < '400' 
|| item.get( 'width' ) > '500' 
|| item.get( 'width' ) === 'null')
{ 
alert("Choose between 400 and 500");
return false; 
}
});
</script>
AmanVirdi
  • 1,667
  • 2
  • 22
  • 32
olzonpon
  • 73
  • 9
  • you should set a `var item_width = item.get('width');` to use in your if statement, instead of repeating `item.get('width')` for each comparison. – Novocaine Aug 05 '14 at 16:19

2 Answers2

0

I dont know how simplecart handles empty strings, but you wrote 'null', which means the entered text needs to be "null", a string, not the object null. Also, an empty string is not null, its an empty string, so try:

simpleCart.bind( 'beforeAdd' , function( item ){
if ( item.get( 'width' ) < '400' 
|| item.get( 'width' ) > '500'
|| item.get( 'width' ) == null
|| item.get( 'width' ) === '')
{ 
alert("Choose between 400 and 500");
return false; 
}
});

Also, testing an integer via < or > against a string is not good programming. It may work but please just code properly:

simpleCart.bind( 'beforeAdd' , function( item ){
if ( item.get( 'width' ) < 400
|| item.get( 'width' ) > 500
|| item.get( 'width' ) == null
|| item.get( 'width' ) === '')
{ 
alert("Choose between 400 and 500");
return false; 
}
});

To make sure item.get( 'width' ) actually gives you an integer, you should parse them:

...
parseInt( item.get('width') ) < ...
...
Alex
  • 9,911
  • 5
  • 33
  • 52
0

try this,

 if( item.get( 'width' ) < '400' || item.get( 'width' ) > '500' || item.get( 'width' ) === 'null' || item.get( 'width' ) === '')
  {   alert("Choose between 400 and 500");   }
hari
  • 1,874
  • 1
  • 16
  • 10