* 1:通过构造函数创建对象
* 2:字面量的方式创建对象
*
* */
var reg=new RegExp(/\d{5}/);
var str="我的电话号码是10086";
//调用方法验证字符串是否匹配
var flag=reg.test(str);
console.log(flag);//true
//字面量的方式创建正则表达式对象
var reg1=/\d{1,5}/;
var flags=reg1.test("我的幸运数字是:777");
console.log(flags);
//验证密码的强度
<style type="text/css">
#dv{
width: 300px;
height:200px;
position: absolute;
left:300px;
top:100px;
}
.strengthLv0 {
height: 6px;
width: 120px;
border: 1px solid #ccc;
padding: 2px;
}
.strengthLv1 {
background: red;
height: 6px;
width: 40px;
border: 1px solid #ccc;
padding: 2px;
}
.strengthLv2 {
background: orange;
height: 6px;
width: 80px;
border: 1px solid #ccc;
padding: 2px;
}
.strengthLv3 {
background: green;
height: 6px;
width: 120px;
border: 1px solid #ccc;
padding: 2px;
}
</style>
</head>
<body>
<div +(this.value.length>=6?getLvl(this.value):0);
};
//给我密码,我返回相应的级别
function getLvl(pwd) {
var lvl=0;//默认级别
//密码中是否有数字,或者字母,或者特殊符号
if(/[0-9]/.test(pwd)){
lvl++;
}
//判断密码中有没有字母
if(/[a-zA-Z]/.test(pwd)){
lvl++;
}
//判断密码中有没有特殊符号
if(/[^0-9a-zA-Z_]/.test(pwd)){
lvl++;
}
return lvl;
}
//验证用户输入的是不是邮箱
请您输入邮箱地址:<input type="text" value="" ;
}
}
//验证表单
<style type="text/css">
body {
background: #ccc;
}
label {
width: 40px;
display: inline-block;
}
span {
color: red;
}
.container {
margin: 100px auto;
width: 400px;
padding: 50px;
line-height: 40px;
border: 1px solid #999;
background: #efefef;
}
span {
margin-left: 30px;
font-size: 12px;
}
.wrong {
color: red
}
.right {
color: green;
}
.defau {
width: 200px;
height: 20px;
}
.de1 {
background-position: 0 -20px;
}
</style>
</head>
<body>
<div class="container" ;
}
}
}
</script>