为什么 ColdFusion 认为值“7”是是一个有效的整数值,我如何验证它不是?
我有一个供用户输入数量的表格。该表单具有客户端验证,以确保该值是整数并且在给定范围内。操作页面具有服务器端验证,以确保该值是整数且大于零。
但是,一种类型的值通过了验证,导致我的 INSERT/UPDATE 查询抛出异常。该值是带有加号的整数 - 即“7+”或“12+”。
当输入这样的值时,ColdFusion 生成的 JavaScript 验证会引发 JavaScript 错误:
_CF_checkformAddToCart = function(_CF_this)
{
//reset on submit
_CF_error_exists = false;
_CF_error_messages = new Array();
_CF_error_fields = new Object();
_CF_FirstErrorField = null;
//form element itemQuantity 'INTEGER' validation checks
if (!_CF_checkinteger(_CF_this['itemQuantity'].value, false))
{
_CF_onError(_CF_this, "itemQuantity", _CF_this['itemQuantity'].value, "Error on itemQuantity, please enter an integer value for quantity that is not greater than 500");
_CF_error_exists = true;
}
//form element itemQuantity 'RANGE' validation checks
if (!_CF_checkrange(_CF_this['itemQuantity'].value, 0.0,500.0, false))
{
_CF_onError(_CF_this, "itemQuantity", _CF_this['itemQuantity'].value, "Error on itemQuantity, please enter an integer value for quantity that is not greater than 500");
_CF_error_exists = true;
}
}
一旦我取消错误弹出窗口,它就会转到操作页面,我在其中[尝试]验证该值,如下所示:
<cfif IsValid("integer", form.itemQuantity) AND form.itemQuantity GT 0>
<cfquery>
INSERT ....
但是,如果尝试这...
<cfset x = Int("7+") />
...ColdFusion 抛出错误。
它是整数还是不是ColdFusion???
如何解决这个问题并正确验证我的表单输入?
I have a form for users to input quantities. The form has client-side validation to ensure that the value is an integer and within a given range. The action page has server-side validation to ensure that the value is an integer and greater than zero.
However, one type of value gets through the validation and is causing my INSERT/UPDATE queries to throw exceptions. That value is an integer with a plus-sign - ie "7+" or "12+".
When such a value is entered, the ColdFusion-generated JavaScript validation throws a JavaScript error:
_CF_checkformAddToCart = function(_CF_this)
{
//reset on submit
_CF_error_exists = false;
_CF_error_messages = new Array();
_CF_error_fields = new Object();
_CF_FirstErrorField = null;
//form element itemQuantity 'INTEGER' validation checks
if (!_CF_checkinteger(_CF_this['itemQuantity'].value, false))
{
_CF_onError(_CF_this, "itemQuantity", _CF_this['itemQuantity'].value, "Error on itemQuantity, please enter an integer value for quantity that is not greater than 500");
_CF_error_exists = true;
}
//form element itemQuantity 'RANGE' validation checks
if (!_CF_checkrange(_CF_this['itemQuantity'].value, 0.0,500.0, false))
{
_CF_onError(_CF_this, "itemQuantity", _CF_this['itemQuantity'].value, "Error on itemQuantity, please enter an integer value for quantity that is not greater than 500");
_CF_error_exists = true;
}
}
Once I cancel out of the error popup, it goes to the action page, where I [try to] validate the value like so:
<cfif IsValid("integer", form.itemQuantity) AND form.itemQuantity GT 0>
<cfquery>
INSERT ....
However, if try this...
<cfset x = Int("7+") />
...ColdFusion throws an error.
Is it an integer or not ColdFusion???
How can get around this and validate my form input correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
isNumeric(form.itemQuantity)
对于“7+”将返回 false,因此要完全验证您的输入为 int,您可以这样做isNumeric(form.itemQuantity)
will return false for "7+", so to fully validate your input as an int, you can do this由于 ColdFusion 无类型的奇怪而奇妙的本质。它不知道您正在使用什么类型的数据,并且会尝试猜测。
它评估 7+ 是有效的。 ColdFusion 中内置的验证做出了很多假设和猜测。
我的建议是不要使用它并编写自己的验证例程,可以对其进行增强以执行您需要的任何操作。
例如,
用户输入
2,075
这是有效还是无效。好吧,如果您有自己的验证,您可以决定,您可以说确定这是一个整数并远程,或者您可以说不,他们不能这样做。
这是一笔很小的前期投资,从长远来看会带来回报。
Due to the weird and wonderful nature of ColdFusion being typeless. It doesn't know what type of data you are working with and it tries to guess.
Its evaluating that 7+ is a valid. The validation built into ColdFusion makes a lot of assumptions and guesses.
My advise would be to not use it and to write your own validation routines that can be enhanced to do whatever you require.
For example
A user enters
2,075
Is this valid or invalid. Well if you have your own validation you can decide, you can say sure this is an integer and remote the , or you can say no they can't do that.
It's a small investment upfront that will pay off in the long run.
事实证明我可以使用 LSParseNumber() 将其转换为有效整数。所以,现在我正在测试一个有效的整数,然后在尝试任何数据库插入之前使用 LSParseNumber() 重置它:
我想我必须重新设计前端客户端验证才能正确验证。
Turns out I can use LSParseNumber() to convert it to valid integer. So, now I'm testing for a valid integer, then resetting it using LSParseNumber() before attempting any database inserts:
I guess I'll have to re-engineer the front-end client-side validation to properly validate.