需要根据文本字段中的值是否为特定数字返回 true 或 false

发布于 2024-09-13 04:55:46 字数 535 浏览 7 评论 0原文

如果用户在输入字段中输入 4,我希望返回 true。

function validateAddition() {
  if($("#validateAddition").val() == 4) {
    return true;
  } else {
    return false;
  }
}

<input value="" 
class="validate[required,onlyNumber,length[0,1]funcCall[validateAddition]] text-input" 
type="text" id="validateAddition" name="validateAddition" />

将其添加到英文js文件中:

"validateAddition":{
    "nname":"validateAddition",
    "alertText":"* Your math is off, try again."}

这应该没问题,让我知道你认为哪里不对。

I am looking to return true if the user entered 4 into the input field.

function validateAddition() {
  if($("#validateAddition").val() == 4) {
    return true;
  } else {
    return false;
  }
}

<input value="" 
class="validate[required,onlyNumber,length[0,1]funcCall[validateAddition]] text-input" 
type="text" id="validateAddition" name="validateAddition" />

Added this into the english language js file:

"validateAddition":{
    "nname":"validateAddition",
    "alertText":"* Your math is off, try again."}

This should be fine, let me know what you think is wrong.

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

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

发布评论

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

评论(4

ぃ双果 2024-09-20 04:55:46

MooGoo

return (parseInt($('#validateAddition').val()) == 4)

编辑: 是正确的,就我的解释而言,我错了。

然而使用 +val 的想法可能不是最好的。虽然上面的方法将起作用,因为您需要更好的解决方案,如下所示:

if(($('#validateAddition').val()-0) == 4)

我们想要 -0 而不是 +0 的原因很简单,因为如果它是一个字符串,并且您想要执行任何其他操作,以防它是,您将不再拥有原始字符串。

http://www.w3schools.com/jsref/jsref_parseInt.asp

return (parseInt($('#validateAddition').val()) == 4)

EDIT: MooGoo is correct, I was mistaken as far as my explanation.

However is idea of using +val is probably not the best. While the method above WILL work as you require a much better solution would be as follows:

if(($('#validateAddition').val()-0) == 4)

The reason we want to -0 as opposed to +0 is simply because if it is a string and you want to perform any additional operations incase it is, you will no longer have the original string.

じ违心 2024-09-20 04:55:46

如果您还想允许其他字符并仅在输入字段的文本包含 4 时返回 true,您应该这样做:

if($("#validateAddition").val().indexOf('4') != -1) {

If you want to allow other characters as well and return true only if the text of input field contains 4, you should do like this instead:

if($("#validateAddition").val().indexOf('4') != -1) {
耀眼的星火 2024-09-20 04:55:46

将值与字符串进行比较是有意义的,因为该值是字符串。这允许您使用严格等于来避免不必要的自动转换。您还可以简化功能。

function validateAddition() {
  return $("#validateAddition").val() === "4";
}

It would make sence to compare the value to a string, since the value is a string. That allows you to use strict equals avoiding unnecessary automatic conversions. Also you can simplify the function.

function validateAddition() {
  return $("#validateAddition").val() === "4";
}
装迷糊 2024-09-20 04:55:46

您可以将验证放入语言文件中,如下所示


"validateAddition": {
    "regex": /[0-3]|[5-9]/,//something that matches on all numbers but four
    "alertText": "* Your math is off, try again."
},


You can put your validation in the language file, looking something like this


"validateAddition": {
    "regex": /[0-3]|[5-9]/,//something that matches on all numbers but four
    "alertText": "* Your math is off, try again."
},


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