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).
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
一旦确认您正在处理一个数字,就可以(如有必要)将其粘贴(如下):
function pad(num, size) {
var s = "000" + num;
return s.substr(s.length-size);
}
要处理填充号码,实际上您必须将值转换为字符串。
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.
发布评论
评论(3)
数字
0100
将被视为八进制值(即十进制的64
)。我认为你只需与字符串进行比较就可以了:
否则你必须将
x
转换为数字并将其与100
进行比较:(实际上你不必显式转换
x
但它也没有什么坏处,并且可以更清楚地显示您正在比较的数据类型)更新: 并且您必须替换
&& 与
||
可以看到。The number
0100
will be treated as octal value (which is64
in decimal).I think you would do fine by just comparing against strings:
Otherwise you have to convert the
x
into a number and compare it against100
:(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.要检查
x
是否在0001
和0099
之间,您可以这样做To check if
x
is between0001
and0099
, you can do this尝试这些方法的组合。
您想先检查您的号码实际上是一个数字(即不包含字母等)。通过调用此方法来确认您的数字是有效的数字值来执行此操作。
一旦确认您正在处理一个数字,就可以(如有必要)将其粘贴(如下):
要处理填充号码,实际上您必须将值转换为字符串。
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.
Once confirmed that you're dealing with a number, you can pad it (if necessary) like this:
To work with padded numbers, you actually have to convert the value to a string.