为了账号安全,请及时绑定邮箱和手机立即绑定

js中正则表达式作为参数怎么写的?

js中正则表达式作为参数怎么写的?

墨色风雨 2018-10-24 15:10:47
代码如下:jsfunction QQ(id,k){var i=document.getElementById(id);if(!k.test(i)){if(id=="QQId"){alert("QQ格式错误或账号位数不对!!!");}if(id=="QQpass"){alert("密码格式错误或位数不对!!!");}}}html<div style=" margin-top:10px"><li style=" list-style:none; width:160px; margin:0px auto; font-size:12px; height:40px">QQ号码:<input type="text" class="input" id="QQId" onblur="QQ('QQId',/^[1-9][0-9]{3,9}$/)" /></li><li style=" list-style:none; width:160px; margin:0px auto; font-size:12px; height:30px">QQ密码:<input type="text" class="input" id="QQpass" onblur="QQ('QQpass',/^([a-zA-Z]{1,}\w){6,12}$/)"/> </li><img src="images/btn_login.gif" style=" margin-left:110px" /><p style=" font-size:12px; margin:10px 5px; line-height:25px"> 世界的改变不是少数人做了很多,而是每个人都做了一点点。伸出手,让我们把爱传递。</p></div>不知道正则表达式作为参数时该怎么用?
查看完整描述

1 回答

?
温温酱

TA贡献1752条经验 获得超4个赞

k传字符串就行了,你在test方法调用之前,先用此字符串new一个正则表达式对象出来就行了,
var t = new RegExp(k, 'g');
t.test(i)
这样就可以了。

查看完整回答
反对 回复 2018-11-02
  • 1 回答
  • 0 关注
  • 771 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信