|
<html>
<head>
<script type= "text/javascript" >
function show()
{
window.alert( "hh" );
}
window.onload= function ()
{
var buttonElement=document.forms[0].mybtn;
buttonElement.onclick=show;
}
function validateForm()
{
var loginform=document.forms[1];
var username=trim(loginform.username.value);
var password=trim(loginform.password.value);
var email=trim(loginform.email.value);
if (username.length==0)
{
alert( "用户必填" );
}
else if (!/^[a-zA-Z0-9]+$/.test(username))
{
alert( "用户名必须是英文字母" );
}
else if (password.length==0)
{
alert( "密码必填" );
}
else if (!/^[0-9]{6}$/.test(password))
{
alert( "密码必须为6位数字" );
}
else if (email.length==0)
{
alert( "邮箱必填" );
}
else if (!/^\\w+@\\w+(\\.\\w+)+$/.test(email))
{
alert( "邮箱格式不正确" );
}
return false ;
}
function trim(s)
{
s=s.replace(/^\\s*$/, "" );
return s;
}
</script>
<style type= "text/css" >
.myclass{
position: absolute;
left: 400px;
top: 150px;
}
</style>
</head>
<body>
<div class= "myclass" >
<form action= "#" name= "myform" method= "post" >
<input type= "button" value= "单机" name= "mybtn" />
</form>
</div>
<!--登录页面的表单-->
<form action= "#" name= "loginform" method= "post" onsubmit= "return validateForm()" >
<table border= "1" align= "center" >
<caption>基于js的验证</caption>
<tr>
<th>用户名:</th>
<td><input type= "text" name= "username" /></td>
</tr>
<tr>
<th>密码:</th>
<td><input type= "password" name= "password" /></td>
</tr>
<tr>
<th>邮箱:</th>
<td><input type= "text" name= "email" /></td>
</tr>
<tr>
<td align= "center" colspan= "2" >
<input type= "submit" value= "提交" />
|