且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何仅在javascript中输入字符时获取数字

更新时间:2022-12-01 12:49:51

,您可以使用正则表达式定义控件,以允许用户仅输入数字并获取数字.[0 -9] ...
you can use regex for defining the control to allow the user to enter only the number and get number.. [0-9]...


您可以使用jQuery验证.......
You can use jQuery validation .......


我想您是在问:如何仅从字符串中获取数字字符,而忽略其他所有内容.您可以使用正则表达式,然后将所有匹配项组合在一起,但是当您可以轻松地逐个字符地处理它时,这似乎有点过头了:

I think you''re asking: how to get only the numeric characters out of a string, ignoring everything else. You could use a regex and then stick all the matches together, but that seems like overkill when you can process it character by character easily enough:

string NumbersOnly(string input){
 StringBuilder output = new StringBuilder();
 foreach(char c in input)
  if(c >= ''0'' && c <= ''9'') output.Append(c);
 return output.ToString();
}