telex-wap
telex-wap

Reputation: 852

Integer validation not working as expected

Thanks to some of the answers on this site, I built a function to validate an integer inside a prompt in javascript. I found out how to use isNaN and the result of % in order to meet my needs, but there must be something wrong, because is still not working: This function for validation needs to accept only integers, and as extra bonus, it will also accept a special keyword used for a different purpose later on in the program.

So, previously I had defined:

var value = prompt("Type an integer");

So after that, I made a call for the validation function, and that included three conditions: The validation warning would jump if:

1) The string is not a number

2) The string % 1 is not 0 (means is not an integer)

3) The string is not the special keyword ("extra") which is also valid as input.

The function needs to loop and keep showing the prompt until a valid data is written.

while (isNaN(value) == true && value % 1 != 0 && value != "extra") {
    alert("Please, type an integer");
    var value = prompt("Type an integer");
}

What am I doing wrong? Thank you so much for any ideas. I know the integer validation has been asked many times here, and here I got a few ideas, but I might be missing something...

Upvotes: 1

Views: 536

Answers (4)

ultranaut
ultranaut

Reputation: 2140

I assume that for your purposes @elclanrs' answer is all you need here, and is the simplest and most straightforward, but just for completeness and dubious laughs, I'm pretty sure that the following would also do what you're looking for:

function isAnIntOrExtra(v) {
  if (parseInt(+v) === +v && v !== '') {
    return parseInt(+v);
  }
  else if (v === 'extra') {
    return v;
  }
  else {
    return false;
  }
}

Fiddle here

These should all pass and return an integer in decimal notation:

  • '387' returns 387
  • '-4' returns -4
  • '0' returns 0
  • '2.4e3' returns 2400
  • '0xf4' returns 244

while these should all fail:

  • '4.5' returns false
  • '2.4e-3' returns false
  • '0xgc' returns false
  • '' returns false
  • 'seven' returns false

And the magic-word 'extra' returns 'extra'

Of course, it'll "fail" miserably with values like '1,345', and will probably roll right over octal notation, treating it as though it were decimal notation (depending on the JavaScript engine?), but it could be tweaked to handle those situations as well, but really, you're better off with the regex.

Upvotes: 0

krg
krg

Reputation: 2800

jsFiddle example

    var int = 10;
var str = "10";

var isInt = function(value) {
    return (str === 'extra' || !isNaN(parseInt(value, 16)) || /^\d+$/.test(value));
};

var isIntStrict = function(value) {
    return (isInt(value) && typeof value !== 'string');
}

console.log('false', isInt('kirk'));
console.log('true', isInt(int));
console.log('true', isInt(str));
console.log('true', 'strict - int', isIntStrict(int));
console.log('false','strict - string', isIntStrict(str));
console.log('false','strict - string', isIntStrict('0x04'));
console.log('true','strict - string', isIntStrict(0x04));

Upvotes: 0

elclanrs
elclanrs

Reputation: 94151

You might be complicating things too much... A quick regular expression will do the trick.

while (!/^(\d+|extra)$/i.test(value)) {
  ...
}

Upvotes: 1

A. Matías Quezada
A. Matías Quezada

Reputation: 1906

You typed only one equal at

isNaN(value) = true

Upvotes: 1

Related Questions