如何仅在javascript中输入字符时获取数字 [英] how to get numbers only while entering characters in javascript

查看:100
本文介绍了如何仅在javascript中输入字符时获取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生,


我只需要数字,


示例:我的输入是(044)(2000023),并将其存储在数据库中.
只会得到0442000023.


如何编写代码


请告诉我

by mohan

Dear sir,


I need only number,


example: my input is (044)(2000023) and stored it in database.
and get only 0442000023.


How to write code


please tell me

by mohan

推荐答案

,您可以使用正则表达式定义控件,以允许用户仅输入数字并获取数字.[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();
}


这篇关于如何仅在javascript中输入字符时获取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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