user2738336
user2738336

Reputation:

Comparing values with leading zeros in Javascript

I have a javascript code that compares two values:

} else if (!(parseInt($('#form_value').val()) >= 1)){
    alert("Error: You didn't pick a number!");

form_value in this case is 001, and I would like to compare it to the one, but it doesn't seem to work. I have tried using parseInt but it didn't work either. Any solutions?

Upvotes: 0

Views: 1304

Answers (3)

KooiInc
KooiInc

Reputation: 122956

May be you should change the condition to if ( +( $('#form_value').val() ) < 1 ) or just if (!+$('#form_value').val()).

Upvotes: 0

Paul D&#39;Ambra
Paul D&#39;Ambra

Reputation: 7824

Well, Number("001"); returns 1 and Number("000"); returns 0

based on your comment above

"I'm trying to display an error if the value is less than 1, the lowest value a user can submit is 000 (which is loaded by default), if you pick something, it becomes 001."

If the lowest possible value is 0 then just test for 0...

var thing = Number($('#form_Value').val());
if (isNaN(thing) || thing === 0) {
  alert('an error message')'
}

Upvotes: 1

user2417483
user2417483

Reputation:

Try:

if (!(Number(parseInt($('#form_value').val(),10)) >= 1)){

EDIT: try this shortened version:

if ( parseInt($('#form_value').val(),10) < 1){

Upvotes: 2

Related Questions