如何验证仅包含数字而不带正则表达式的文本框 [英] How can I validate a textbox which can take only numbers without regular expressions

查看:134
本文介绍了如何验证仅包含数字而不带正则表达式的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

海,

我需要验证我的文本框,该文本框仅包含数字而不包含小数.我不需要正则表达式,我需要服务器端编码.帮助我提出您的想法

Hai,

I need to validate my textbox which takes only numbers without decimals. I don''t want regular expressions, I need server side coding. Help me with your ideas

推荐答案



请尝试以下javascript,
Hi,

Try below javascript,
 function checkNumeric(x)
 {

   var len=x.value.length ;
   var charcode = 0;
     for (var i=0;i<len;i++)
     {
      charcode = x.value.charCodeAt(i);
      if(!((charcode>=48 && charcode<=57)))
      {
         alert("Only Numeric Values Allowed");
          x.value='';
         x.focus();
        return false;
     }
    }
    return true;
}



<input type="text" id="txtfield" onChange='checkNumeric(this)'/>



希望对您有帮助
所有d最佳



I hope this will help you
All d Best


在此处了解所有信息: http://msdn.microsoft.com/en-us/library/aa479013.aspx [ ^ ].
书也是好朋友,因此您可能应该去找一本书.

加油!
Read all about it here: http://msdn.microsoft.com/en-us/library/aa479013.aspx[^].
Books also are good friends, so you should probably go and find one.

Cheers!


如果只需要Integer值,则可以使用 int.TryParse(string,outputMember) [ ^ ].根据输入的大小,您可能需要使用Int64.TryParse()

如果数字太大而连Int64都无法处理,则必须使用Char.IsNumeric()方法类似的方法逐个字符地测试字符串,并检查小数点,逗号等.

我刚刚找到了有关此的文章- VB的IsNumeric()的C#等效版本 [ ^ ].
If you only want Integer values you could use the int.TryParse(string, outputMember)[^]. Depending on the size of the input, you might need to use Int64.TryParse()

If the number is too large for even Int64 to handle, you will have to test the string character by character, using something like the Char.IsNumeric() method and check for decimal points, commas etc. as well.

I have just found an article about this - C# Equivalent of VB''s IsNumeric()[^].


这篇关于如何验证仅包含数字而不带正则表达式的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆