此文适合网页初学者以及对制作网页感兴趣的朋友,感谢您的阅读噢~
废话不多说,先上效果图:
图①:输入不合规范时的提示
图②:登录成功弹出提示框
图③:登录失败的提示框
以下是代码:
body {background-color: pink}
form { position:absolute;
left:500px;
top:200px;}
.like1 {
border:3px solid green;
border-radius:18px;
width: 300px;
height: 30px;
outline:none;
}
.like2 {
border:3px solid white;
border-radius:18px;
background:green;
color: white;
width: 60px;
height: 30px;
outline:none;
}
function ys(x)
{x.style.background="yellow";}
function yhname()
{
document.getElementById("name").style.background="white";
var name = document.getElementById("name").value;
var tip = document.getElementById("namespan");
var guize = /^([\u4e00-\u9fa5]|[a-zA-Z0-9]){1,10}$/i;
if (guize.test(name)) {
tip.innerHTML=" " ;
return true; }
else {
tip.innerHTML="提示:用户名不规范!只能包含汉字、数字、字母,不能含有其它字符".fontcolor("red");
return false;
}
}
function mima()
{
document.getElementById("password").style.background="white";
var password = document.getElementById("password").value;
var pw = document.getElementById("pwspan")
var guize2 = /^([a-zA-Z0-9]){6,15}$/i;
if (guize2.test(password)) {
pw.innerHTML=" ";
return true ;
}
else {
pw.innerHTML="密码不规范!长度须在6到15位,只能包含字母、数字,不可以含有其他符号".fontcolor("red");
return false ;
}
}
function alll()
{
if (yhname()==true&&mima()==true) {
alert("恭喜您,登录成功!")
return true;
}
else {
alert("登录失败!请重新填写信息!")
return false;
}
}
代码说明:
首先是style标签里面的设置,第6行是背景color,第7行是组件位置,用了绝对定位,把文字和输入框定在网页中间的位置;like1是输入框的样式,like2是登录按钮的样式。
然后是script里面的函数,function ys(x)设置x的背景颜色是黄color,用于input的onfocus,即鼠标点到输入框的时候会显示黄color。而function yhname() 用于检测用户输入的用户名,使用正则表达式进行判别,同样地,function mima() 亦是利用正则表达式判断密码是否符合规范;最后的function alll()用于提示用户登录成功或者失败,如果输入的信息符合规范则弹出登录成功的框,否则提示登录失败,需要重新输入。
再来就是body标签里的代码,这里是应用上面写的样式以及函数。
—完--- 有疑问欢迎留言!