需要根据文本字段中的值是否为特定数字返回 true 或 false
如果用户在输入字段中输入 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
MooGoo
编辑: 是正确的,就我的解释而言,我错了。
然而使用 +val 的想法可能不是最好的。虽然上面的方法将起作用,因为您需要更好的解决方案,如下所示:
我们想要 -0 而不是 +0 的原因很简单,因为如果它是一个字符串,并且您想要执行任何其他操作,以防它是,您将不再拥有原始字符串。
http://www.w3schools.com/jsref/jsref_parseInt.asp
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:
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.
如果您还想允许其他字符并仅在输入字段的文本包含 4 时返回 true,您应该这样做:
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:
将值与字符串进行比较是有意义的,因为该值是字符串。这允许您使用严格等于来避免不必要的自动转换。您还可以简化功能。
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.
您可以将验证放入语言文件中,如下所示
You can put your validation in the language file, looking something like this