I want to compare the two input values, just try in javascript, but it's not working fine. I'm using the following code
function check_closing() { var opening = $('#opening').val(); var closing = $('#closing').val(); if(opening > closing) { alert('Opening is greater than Closing. Please enter the correct value'); $('#closing').val(''); } }
if the opening value input = 8541, closing value like = 8241 it's work fine, but if the closing is 954 it's not working. Please help.
Thanks in advance.
You're comparing the strings instead of integers, so you need to convert the strings to integers. Since as strings, '8541' > '8241'
>>>'8541' > '8241' trueInput values are always strings. To compare them as numbers, you must convert them to numbers. You can do that with:
parseIntif they're whole numbers and you want to specify the number base and stop parsing at the first non-numeric characterparseFloatif they're decimal fractional numbers- The
+if you want JavaScript to guess the number base, and give youNaNif there's a non-numeric character
...and a few others.
Example:
var opening = parseInt($('#opening').val(), 10);Try this..
function check_closing() { var opening = $('#opening').val(); var closing = $('#closing').val(); if(parseInt(opening) > parseInt(closing)) { alert('Opening is greater than Closing. Please enter the correct value'); $('#closing').val(''); } }
No comments:
Post a Comment