验证带小数的数字 [英] Validation for Numeric with decimal
本文介绍了验证带小数的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我想验证文本框值,我应该允许用户输入像
这样的值
1,
1.5,
2,
2.5
它不应该取十进制之后的其他0或5,
它应该允许10000.5。
预付款
Vishwa
解决方案
使用方法System.Decimal.TryParse
;如果它返回true,则该字符串作为十进制表示有效:
http://msdn.microsoft.com/en-us/library/system.decimal.tryparse.aspx [ ^ ]。
之后,取出解析后的值另外检查所需的范围或任何其他约束。
好的,对于客户端,这是一个JavaScript变种。检查数值是否有效更加棘手:
function isValidFloat(stringValue){
value = parseFloat (stringValue); // 如果无效,将返回NaN
返回(value == value); // 检查NaN的值
}
注意:没有十进制,只有浮点数和整数。
完成后,检查你的约束方式相同。
-SA
使用此代码:
< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = 服务器 >
< title > < / title >
< / head >
< body >
< 表格 id = form1 runat = server >
< div < span class =code-keyword>>
< / div >
< asp:TextBox ID = TextBox1 runat = server > < / asp:TextBox >
< asp:CompareValidator ID = 比较Validator1 runat = server
ControlToValidate = TextBox1 ErrorMessage = CompareValidator 类型 = Dou ble > 输入十进制值< / asp:CompareValidator >
< asp:Button ID = button1 文字 = 验证 runat = server / >
< / form >
< / body >
< / html >
Hi guys
I want to validate a text box value, I should allow users to enter values like
1,
1.5,
2,
2.5
it should not take other then 0 or 5 after decimal,
it might be like 10000.5 it should allow.
Advance thanks
Vishwa
解决方案
Use the methodSystem.Decimal.TryParse
; if it returns true, the string is valid as a decimal representation:
http://msdn.microsoft.com/en-us/library/system.decimal.tryparse.aspx[^].
After that, take parsed value and additionally check up for required range or any other constraint.
[EDIT]
OK, for client side, this is a JavaScript variant. Checking up if the numeric value is valid is more tricky:
function isValidFloat(stringValue) { value = parseFloat(stringValue); // will return NaN if invalid return (value == value); // check value for NaN }
Note: there is not "decimal", only floats and integers.
When this is done, check for your constraints the same way.
—SA
Use This Code:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> </div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="CompareValidator" Type="Double">Enter Decimal Value</asp:CompareValidator> <asp:Button ID="button1" Text="validate" runat="server"/> </form> </body> </html>
这篇关于验证带小数的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文