如何验证asp.net中的文本框 [英] how to validate textbox in asp.net
问题描述
大家好,请帮帮我,
我必须通过正则表达式或javascript验证asp.net中的文本框.
该用户不能输入任何字母.他们只能在文本框中输入小数.
hi all please help me,
i have to validate textbox in asp.net through regular expression or javascript
that user can not enter any alphabets. They can only enter decimal number into textbox.
推荐答案
尝试以下操作:
try this:
<asp:textbox id="txt_qty" runat="server" columns="2"></asp:textbox>
<asp:requiredfieldvalidator id="r1" runat="server" errormessage="*" controltovalidate="txt_qty"></asp:requiredfieldvalidator>
<asp:regularexpressionvalidator id="regular1" controltovalidate="txt_qty" runat="server" errormessage="*" validationexpression="^\d+
" > /asp:regularexpressionvalidator
这样,您的问题将得到解决...
this way your problem will solve...
您可以通过以下方式完成此操作:
1.AjaxFilteredTextBoxExtender
在这里,您首先添加ajax工具包和此扩展程序的参考.
您可以将ValidChars设置为."用于十进制,允许用户输入带小数点的数字值.
You can done this in following ways:
1.AjaxFilteredTextBoxExtender
here you first add reference of ajax toolkit and this extender.
you can set ValidChars to "." for decimal which allo user to enter numeric value with decimal point.
<ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server" TargetControlID="TextBox3" FilterType="Custom, Numbers" ValidChars="." />
2.使用javscript
< script type ="text/javascript">
//价格文本框只允许数字,只允许2个小数点
函数extractNumber(obj,decimalPlaces,allowNegative)
{
var temp = obj.value;
//如果格式正确,请避免更改内容
var reg0Str =''[0-9] *'';
if(decimalPlaces> 0){
reg0Str + =''\\.?[0-9] {0,''+小数位数+''}'';
}否则,如果(decimalPlaces< 0){
reg0Str + =''\\.?[0-9] *'';
}
reg0Str = allowNegative吗? ''^-?''+ reg0Str:''^''+ reg0Str;
reg0Str = reg0Str +''
2.using javscript
<script type="text/javascript">
// price text-box allow numeric and allow 2 decimal points only
function extractNumber(obj, decimalPlaces, allowNegative)
{
var temp = obj.value;
// avoid changing things if already formatted correctly
var reg0Str = ''[0-9]*'';
if (decimalPlaces > 0) {
reg0Str += ''\\.?[0-9]{0,'' + decimalPlaces + ''}'';
} else if (decimalPlaces < 0) {
reg0Str += ''\\.?[0-9]*'';
}
reg0Str = allowNegative ? ''^-?'' + reg0Str : ''^'' + reg0Str;
reg0Str = reg0Str + ''
这篇关于如何验证asp.net中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!