0

i hate asking for help as i would rather figure things for myself, or learn from what others have posted or already asked and solved. as such this is my first post on here!

This is either really really simple and im over complicating things or im going about it the wrong way. ive been searching everywhere for over 2 hours now. im not exactly a noob at js but i am still sort of new,i would say learning, ameteur?? anywho....

what i am trying to do:

so i have a number input box <input type="number" maxlength="4" .../> where a user will enter a 4 digit number (needs to be 4 digits) from 100 to 8000. so obviously 100 to 999 would be 0100 - 0999.

no problem so far, i can just use a string to pass the variable through as 4 digits.

the problem i have is that i need to add certain conditions to validate the input. one condition is bugging me. this is what i have so far:

(var x is already set to the form and number input box)

if (x=="" || isNaN(x) || x.length!== 4 && x< 0100 || x>8000)
{alert(please enter the correct amount!}
else {run rest of function}

so they all work execpt for:x<0100

if the user enters 99 it flags up because it is not 4 digits and if they enter 0099 it accepts it and runs the rest of the function code.

i just need it to alert for any amount from 0001 to 0099.

i found another post, How to make 8 digit number in javascript? , sort of relevant but the guy just wants to output a number padded with zeros. i think one of the solutions (code below) may be of use to me but as i am rather tired, brain frazzled and new i cant solve it:

var i = (100).toPrecision(8).split('.').reverse().join('');

i would start by editing it to:

var i = (x).toPrecision(4).split('.').reverse().join('');

(but would this only work if they typed 99 not 0099...if ya see what i mean)

i think it would be like a reverse of the code (split 0099 to 99.00) , and then the statement would be: if(.... && i<==99 || ....) but idk how to write it in JS...

ok so do ya see how this is messing with me mind, being a semi/quarterly noob and all!!

sorry its not formatted correctly and so long, i havent grasped how to use the code functions...

and thanks for your patience in reading this (if you got this far hehe).

THANKS IN ADVANCE

Slappy-x

4

3 回答 3

2

The number 0100 will be treated as octal value (which is 64 in decimal).

I think you would do fine by just comparing against strings:

x < "0100" || x > "8000"

Otherwise you have to convert the x into a number and compare it against 100:

+x < 100 || +x > 8000

(actually you don't have to explicitly convert x but it does not hurt either and makes it clearer which data types you are comparing)

Update: And you have to replace && with || as far as I can see.

于 2011-09-12T00:47:28.530 回答
0

To check if x is between 0001 and 0099, you can do this

if (x.match(/00([1-9][0-9]|0[1-9])/))
于 2011-09-12T00:45:36.347 回答
0

Try a combination of these methods.

You want to check that your number is actually a number first (i.e. doesn't contain letters, etc.). Do this by calling this method to confirm your number is a valid numeric value.

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

Once confirmed that you're dealing with a number, you can pad it (if necessary) like this:

function pad(num, size) {
    var s = "000" + num;
    return s.substr(s.length-size);
}

To work with padded numbers, you actually have to convert the value to a string.

于 2011-09-12T00:50:36.287 回答