1

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...

2
  • = is an assignment operator, the equality comparison operator is ==. Commented Oct 7, 2012 at 22:14
  • Very true, silly me... but after correction, still not working Commented Oct 7, 2012 at 22:42

4 Answers 4

1

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

while (!/^(\d+|extra)$/i.test(value)) {
  ...
}
Sign up to request clarification or add additional context in comments.

5 Comments

I might be overthinking it, and I'm not sure if the question is for @elclanrs or the op, but what happens with something like value = "0x04" ?
Thank you for the tip! I am very novice in this, just starting with javascript after fiddling a bit with php, and didn't know about regular expressions...
No, I realize that, but it is a (hexadecimal) integer. It's entirely likely that he doesn't want to accept anything other than base 10 numbers, in which case your solution is straightforward and simple, and probably better than going down the original path. But if there is a reason to allow hex, etc, then the regex method isn't going to work as it is.
Actually, I guess the only possibility other than decimal or hex would be octal, e.g. 064, fwiw.
Oh I see what you mean, yeah don't know OP's needs but for the octal case you can prevent the zero I guess ^[^0](\d+|extra)$
1

You typed only one equal at

isNaN(value) = true

3 Comments

Thanks! totally right... but still, that was not the main problem. After correcting that silly mistake, if for example I input the number 2 (which should be valid), it detects it as invalid and prompts me to input it again...
@A.MatíasQuezada it seems to be working a little too well, e.g. '3.4' seems to pass the test. ;)
Lol, thats right, I think it's better to use the regex @elclanrs wrote.
0

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));

1 Comment

Thanks for all the ideas. Even though my purpose is much simpler, I am learning a lot with all your comments.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.