javascript:尝试验证必须为 4 位数字的数字,以查看 x<100(即 0100)

发布于 2024-12-04 07:55:06 字数 1512 浏览 0 评论 0 原文

我讨厌寻求帮助,因为我宁愿自己解决问题,或者从其他人发布的或已经询问和解决的内容中学习。因此,这是我在这里发表的第一篇文章!

这要么非常非常简单,但我把事情变得过于复杂,要么我以错误的方式处理它。我已经到处找了两个多小时了。我不完全是 js 新手,但我还是个新人,我会说学习,业余爱好者?任何人....

我想要做什么:

所以我有一个数字输入框 用户将在其中输入从 100 到 8000 的 4 位数字(需要是 4 位数字)。显然 100 到 999 将是 0100 - 0999。

到目前为止没有问题,我可以只使用字符串将变量作为 4 位数字传递。

我遇到的问题是我需要添加某些条件来验证输入。有一个条件困扰着我。这就是我到目前为止所拥有的:

(var x 已经设置为表单和数字输入框)

if (x=="" || isNaN(x) || x.length!== 4 && ;x<0100||x>8000)
{提醒(请输入正确的金额!}
else {运行函数的其余部分}

所以它们都可以工作:x<0100

如果用户输入 99,它会标记出来,因为它不是 4 位数字,如果他们输入 0099,它会接受它并运行函数的其余部分代码。

我只需要它来提醒从 0001 到 0099 的任何金额。

我找到了另一篇文章,How to make 8digit number in javascript? ,有点相关,但那家伙只想输出一个用零填充的数字。我认为其中一个解决方案(下面的代码)可能对我有用,但由于我很累,大脑疲惫且新,我无法解决它:

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

我首先将其编辑为:

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

(但这只有在他们输入 99 而不是 0099 时才有效......如果你看到我的内容意思是)

我认为这就像代码的反转(将 0099 拆分为 99.00),然后语句将是: if(.... && i<==99 || ....) 但不知道如何用 JS 编写它...

好吧,你知道这对我来说是多么混乱吗,作为一个半/季度菜鸟等等!

抱歉,它的格式不正确,而且这么长时间,我还没有掌握如何使用代码功能...

感谢您耐心阅读本文(如果您读到这里,呵呵)。

提前致谢

Slappy-x

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苹果你个爱泡泡 2024-12-11 07:55:06

数字0100将被视为八进制值(即十进制的64)。

我认为你只需与字符串进行比较就可以了:

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

否则你必须将 x 转换为数字并将其与 100 进行比较:(

+x < 100 || +x > 8000

实际上你不必显式转换 x 但它也没有什么坏处,并且可以更清楚地显示您正在比较的数据类型)

更新: 并且您必须替换 &&||可以看到。

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.

莫多说 2024-12-11 07:55:06

要检查 x 是否在 00010099 之间,您可以这样做

if (x.match(/00([1-9][0-9]|0[1-9])/))

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

if (x.match(/00([1-9][0-9]|0[1-9])/))
摘星┃星的人 2024-12-11 07:55:06

尝试这些方法的组合。

您想先检查您的号码实际上是一个数字(即不包含字母等)。通过调用此方法来确认您的数字是有效的数字值来执行此操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文