js控制只能输入数字和逗号的正则表达式怎么写? input 输入框只能输入数字和逗号,用正则表达式怎么写
\u5982\u4f55\u7528js\u63a7\u5236\u6587\u672c\u6846\u53ea\u80fd\u8f93\u5165\u6570\u5b57\u548c\u9017\u53f7\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\uff1f\u63d0\u4f9b\u51e0\u4e2a\u601d\u8def\uff1a
1. \u7528HTML5\u4e2dinput\u7684pattern\u5c5e\u6027\uff0c\u4ed6\u662f\u6307\u5b9a\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u8f93\u5165\u7684\u5185\u5bb9\u5fc5\u987b\u7b26\u5408\u3002\u50cf\u8fd9\u79cd\u683c\u5f0f\uff0c\u7528\u300c(\d+,)+\d+\u300d\u8fd9\u6837\u7684\u6b63\u5219\u5c31\u884c\u3002\u7f3a\u70b9\u662fSafari\u548cIE10\u4ee5\u4e0b\u4e0d\u652f\u6301\u3002
2. input\u7684onchange\u4e8b\u4ef6\uff0c\u6bcf\u6b21\u90fd\u83b7\u53d6\u5b57\u7b26\u4e32\uff0c\u53d6\u91cc\u9762\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5224\u65ad\u662f\u5426\u4e3a\u6570\u5b57\u6216\u9017\u53f7\u3002
3. input\u7684onkeyup/ onkeydown\u4e8b\u4ef6\uff0c\u6bcf\u6b21\u6309\u952e\u5c31\u89e6\u53d1\uff0c\u5224\u65ad\u6309\u952e\u4ee3\u7801\u662f\u5426\u4e3a\u6570\u5b57\u952e\u6216\u9017\u53f7\u952e\u3002\u7f3a\u70b9\u662f\u6309\u6570\u5b57\u952e\u4e0d\u4e00\u5b9a\u8f93\u5165\u6570\u5b57\uff0c\u8fd8\u9700\u8981\u4e00\u5757\u5224\u65ad\u662f\u5426\u6309\u4e0bshift\u7b49\u3002
\u8fd8\u662f2\u597d\u4e00\u70b9\u3002
function IsNum(e) {
var key = window.event ? e.keyCode : e.which;
var keychar = String.fromCharCode(key);
reg = /[^0-,]/g;
var result = reg.test(keychar);
if(result)
{
// el.className = "warn";
return false;
}
else
{
// el.className = "";
return true;
}
}
需要准备的材料分别有:电脑、html编辑器、浏览器。
1、首先,打开html编辑器,新建html文件,例如:index.html。
2、在index.html中的<script>标签,输入js代码:。
var reg = /^[0-9,]*$/;
var a = '1231,456,346,', b = '34sda';
test(a);test(b);
function test(text) {
if (reg.test(text)) {
$('body').append(text + '符合</br>');
} else {
$('body').append(text + '不符合</br>');
}
}
3、浏览器运行index.html页面,此时会打印出该正则表达式对“1231,456,346,”和“34sda
”的校验结果。
楼主您好,正则式如下:
变量=/^[\d\,]\d*\,*$/
var reg = /^[0-9,]+$/;
var txt = $("#id").html();
alert(reg.text(txt));
绛旓細1銆侀鍏堬紝鎵撳紑html缂栬緫鍣紝鏂板缓html鏂囦欢锛屼緥濡傦細index.html銆2銆佸湪index.html涓殑鏍囩锛杈撳叆js浠g爜锛氥倂ar reg = /^[0-9,]*$/;var a = '1231,456,346,', b = '34sda';test(a);test(b);function test(text) { if (reg.test(text)) { ('body').append(text + '绗﹀悎');} else...
绛旓細* 楠岃瘉鍙兘杈撳叆鏁板瓧,閫楀彿鍜屽皬鏁扮偣鐨勬鍒欒〃杈惧紡 */ Pattern p = Pattern.compile("[\\d,.]+"); String s = "21534,.dd"; Matcher m = p.matcher(s); System.out.println(m.matches()); 鏈洖绛旂敱缃戝弸鎺ㄨ崘 涓炬姤| 绛旀绾犻敊 | 璇勮 0 5 鍧忓潖濂惰尪 閲囩撼鐜:52% 鎿呴暱: JAVA鐩稿叧 C/C++ 鏁版嵁搴揇B ...
绛旓細
绛旓細([0-9]+[,]?)+
绛旓細'true');} test var a=/^[,]+$/;杩欏彞璇濊В閲婁笅 ^鍖归厤璇彞寮濮 $鍖归厤缁撴潫 [锛宂浠h〃閫楀彿 + 浠h〃鑷冲皯鍑虹幇涓娆
绛旓細JS姝e垯琛ㄨ揪寮 /^,\d\d,$/ 鎴戠粰浣犱竴涓狫avascript鐨勪緥瀛,浣犵湅鐪嬪惂. var str=',23,'; var regex=/^,\d\d,$/ var a=regex.test(str); alert(a);杩愯缁撴灉 true
绛旓細var get=function (){ var reg=/^[A-Za-z0-9]+?$/ return function(s){ var sta=s.split(",");for(var i=0;i<sta.length;i++){ if(!reg.test(sta[i])) sta.splice(i,1);} return sta.join(",");} }();alert(get("1,2,3,w,鎴,4"))...
绛旓細= ''; for(var i=0;i<str.length;i++) { tmp += String.fromCharCode(str.charCodeAt(i)+65248) } return tmp } alert(meizz('abcdefghijklmnopqrstuvwxyz1234567890')); alert(meizz('~`!@#$%^&*()_+|-={}[]:";<>?,./\\\'')); //--> </SCRIPT> ...
绛旓細*(1[7358]\d{9})/杩欐槸澶氫釜鍙风爜鐨勮В閲婏細1[7358]\d{9}#鍖归厤涓涓彿鐮乗s*,\s*#鍖归厤鍙风爜涔嬮棿鐨勯楀彿锛屽彿鐮鍜岄楀彿涔嬮棿鍏佽鏈夌┖鏍间篃灏辨槸\s*(1[7358]\d{9}\s*,\s*)*#鏁翠綋鍚堣捣鏉ワ紝*鏄熷彿琛ㄧず鍙互閲嶅浠绘剰娆★紝涔熷氨鏄彲浠ユ湁澶氫釜鍙风爜(1[7358]\d{9})#鏈鍚庝竴涓敤鏉ュ尮閰嶆病鏈閫楀彿鐨鍙风爜 ...
绛旓細onkeyup="value=value.replace(/[^a-zA-Z\u4e00-\u9fa5,]/g,'')"杩欎釜鏇翠弗璋ㄤ竴浜涳紝LZ閭d簺杩樺彲浠杈撳叆鍏朵粬绗﹀彿