码迷,mamicode.com
首页 > Web开发 > 详细

正则Js

时间:2015-04-09 08:44:52      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script>
var reg = /\d{5}/g;
var msg = ‘中国移动:10086,中国联通:10010,中国电信:10000‘;
//alert(reg.test(msg));//test方法判断的有没有匹配上,返回的bool值
//alert(reg.exec(msg));//在正则表达式后面加一个g
//循环取匹配的值
while ((result=reg.exec(msg)) != null) {
alert(result);
}
onload = function () {
//var reg = /\w+((-w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+/;
//document.getElementById(‘txt‘).onblur = function () {
// //document.getElementById(‘txt‘).value//拿到文本框的值
// if (!reg.test(this.value)) {
// alert(‘不是邮箱‘);
// };
//};
//var msg = ‘14333338888,15844440000,18515880000‘;
//var result= msg.replace(/(\d{3})(\d{4})(\d{4})/g, ‘$1****$3‘);
//alert(result);

var msg = ‘ 123 ‘;
function trimString(msg) {
return msg.replace(/^\s+/, ‘‘).replace(/\s+$/, ‘‘);
}
var res = trimString(msg);
alert(‘==‘+res+‘==‘);
};
</script>
</head>
<body>
邮箱<input type="text" name="name" value=" " id="txt"/>
</body>
</html>

==================================================

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script>
onload = function () {
document.getElementById(‘txt‘).onkeyup = function () {
var tds = document.getElementById(‘tb‘).getElementsByTagName(‘td‘);
for (var i = 0; i < tds.length; i++) {
tds[i].style.backgroundColor = ‘‘;
}
var num = checkPwd(this.value);
if (this.value.length > 0) {

if (num == 3) {
tds[0].style.backgroundColor = ‘blue‘;
tds[1].style.backgroundColor = ‘blue‘;
tds[2].style.backgroundColor = ‘blue‘;
} if (num == 2) {
tds[0].style.backgroundColor = ‘yellow‘;
tds[1].style.backgroundColor = ‘yellow‘;
} if (num <= 1) {
tds[0].style.backgroundColor = ‘red‘;
}
}
};
};
function checkPwd(msg) {
var lvl = 0;
if (msg.match(/[0-9]/)) {
lvl++;
}
if (msg.match(/[a-zA-Z]/)) {
lvl++;
}
if (msg.match(/[^a-zA-Z0-9]/)) {
lvl++;
}
if (msg.length < 6) {
lvl--;
}
return lvl;
}
</script>
</head>
<body>
密码:<input type="text" name="name" value=" " id="txt" /><br />
<table id="tb" border="1" style="width: 300px; height: 30px; font-size: 25px; text-align: center;">
<tr>
<td>

</td>
<td>

</td>
<td>

</td>
</tr>
</table>
</body>
</html>

正则Js

标签:

原文地址:http://www.cnblogs.com/bidianqing/p/4408535.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!