【Java学习20170521】JavaScript复习

本文集提供了一系列实用的JavaScript代码段,包括金额计算、表单验证、日期格式验证、时间显示等,适用于网页开发者快速实现常见功能。

JavaScript代码

01

<script type="text/javascript">
	var price = 992; //定义商品单价
	var number = 10; //定义商品数量
	var sum = price * number; //计算商品金额
	alert(sum); //显示商品金额
</script>
02

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="style.css" rel="stylesheet" />
<script language="javascript">
	function check() {
		if (form1.user.value == "") { //判断用户名是否为空
			alert("请输入用户名!");form1.user.focus();return;
		} else if (form1.pwd.value == "") { //判断密码是否为空
			alert("请输入密码!");form1.pwd.focus();return;
		} else {
			form1.submit(); //提交表单
		}
	}
</script>
</head>

<body>
	<center>
		<form name="form1" method="post" action="">
			<table width="221" border="1" cellspacing="0" cellpadding="0"
				bordercolor="#FFFFFF" bordercolordark="#CCCCCC"
				bordercolorlight="#FFFFFF">
				<tr>
					<td height="30" colspan="2" bgcolor="#eeeeee">用户登录</td>
				</tr>
				<tr>
					<td width="59" height="30">用户名:</td>
					<td width="162"><input name="user" type="text" id="user"></td>
				</tr>
				<tr>
					<td height="30">密  码:</td>
					<td><input name="pwd" type="text" id="pwd"></td>
				</tr>
				<tr>
					<td height="30" colspan="2" align="center"><input
						name="Button" type="button" class="btn_grey" value="登录"
						onClick="check()">   <input name="Submit2"
							type="reset" class="btn_grey" value="重置"></td>
				</tr>
			</table>
		</form>
	</center>
</body>
</html>

body{
	FONT-SIZE: 9pt;
	margin-left:0px;
}
td{
	font-size: 9pt;	color: #000000;
	padding-left:5px;
}
.btn_grey {
	font-family: "宋体";	font-size: 9pt;color: #333333;	
	background-color: #eeeeee;cursor: hand;padding:1px;height:19px;
	border-top: 1px solid #FFFFFF;border-right:1px solid #666666;
	border-bottom: 1px solid #666666;border-left: 1px solid #FFFFFF;
}
input{
	font-family: "宋体";
	font-size: 9pt;
	color: #333333;
	border: 1px solid #999999;

}

03
<script language="javascript">
	var now = new Date(); //获取系统日期
	var day = now.getDay(); //获取星期
	var week;
	switch (day) {
	case 1:
		week = "星期一";
		break;
	case 2:
		week = "星期二";
		break;
	case 3:
		week = "星期三";
		break;
	case 4:
		week = "星期四";
		break;
	case 5:
		week = "星期五";
		break;
	case 6:
		week = "星期六";
		break;
	default:
		week = "星期日";
		break;
	}
	document.write("今天是" + week); //输出中文的星期
</script>


04

<script language="javascript">
var sum=0;
for(i=1;i<100;i+=2){
	sum=sum+i;          
}
alert("100以内所有奇数的和为:"+sum);				//输出计算结果
</script>

05

<script language="javascript">
	var i=1;      					//由于是计算自然数,所以i的初始值设置为1
	var sum=i;
	var result="";
	document.write("累加和不大于10的所有自然数为:<br>");
	while(sum<10){
		sum=sum+i;					//累加i的值
		document.write(i+'<br>');			//输出符合条件的自然数
		i++;	        					//该语句一定不要少
	}
</script>
06

<script language="javascript">
	var sum=0;
	var i=1;      							//由于是计算自然数,所以1的初始值设置为1
	document.write("累加和不大于10的所有自然数为:<br>");
	do{
		sum=sum+i;						//累加i的值
		document.write(i+'<br>');				//输出符合条件的自然数
		i++;	        						//该语句一定不用少
	}while(sum<10);
</script>
07

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>中文姓名验证</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script language="javascript">
	function checkRealName() {
		var str = form1.realName.value; //获取输入的真实姓名
		if (str == "") { //当真实姓名为空时
			alert("请输入真实姓名!");form1.realName.focus();return;
		} else { //当真实姓名不为空时
			var objExp = /[\u4E00-\u9FA5]{2,}/; //创建RegExp对象,RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具。
			//http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp
			if (objExp.test(str) == true) { //判断是否匹配
				alert("您输入的真实姓名正确!");
			} else {
				alert("您输入的真实姓名不正确!");
			}
		}
	}
</script>
</head>

<body>
	<center>
		<form name="form1" method="post" action="">
			请输入真实姓名:<input name="realName" type="text" id="realName" /> <input
				name="Button" type="button" onclick="checkRealName()" value="检测" />
		</form>
	</center>
</body>
</html>
08

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <!-- <base href="<%=basePath%>"> -->
    <title>验证身份证号码是否有效</title>
	<script language="javascript"> 
	function checkIDCard(){
		var IDCard = document.getElementById("IDCard");	
		//验证15位身份证号码
		var regIDCard_15 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/; 
		//验证18位身份证号码 
		var regIDCard_18 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}[\d|x|X]$/; 
		if(IDCard.value.length!=15&&IDCard.value.length!=18){
			alert("您输入的身份证号码长度不对,请输入15位或18位的身份证号码!");
			IDCard.focus();						
			return;		
		}else{
			if(IDCard.value.length==15){//验证15位的身份证号码 
				if(!regIDCard_15.test(IDCard.value)){	
					alert("您输入的身份证号码有误!");
					IDCard.focus();						
					return;								
				}
			}
			if(IDCard.value.length==18){//验证18位的身份证号码 
				if(!regIDCard_18.test(IDCard.value)){
					alert("您输入的身份证号码有误!");
					IDCard.focus();						
					return;		
				}
			}
		}
                        alert("输入正确");
			document.getElementById("myform").submit();
	}
	</script>
	<style type="text/css">
		table{
			font-size: 13px;
			font-family: 楷体;
			color:navy;
		
	
		}
		input{
			font-size: 13px;
			font-family: 楷体;
			color:navy;
		}
		font{
			font-size: 12px;
			font-family: 楷体;
			color:orangered;
		}
	</style>
  </head>
  <body>
   <form action="" id="myform">
   	<table align="center">
   		<tr>
   			<td>姓名:</td>
   			<td>
   				<input type="text"  id="name"> 			
   			</td>
   		</tr>	
   		<tr>
   			<td> 性别:</td>
   			<td>
   				<input type="radio" name="sex" id="man" value="m" />男	
   				<input type="radio" name="sex" id="woman" value="f" />女			
   			</td>
   		</tr>
   		
   		<tr>
   			<td>年龄:</td>
   			<td>
   				<input type="text" id="age">
   			</td>
   		</tr>
   		<tr>
   			<td>电子邮箱:</td>
   			<td>
   				<input type="text" id="email">
   			</td>
   		</tr>
   		<tr>
   			<td>身份证号码:</td>
   			<td>
   				<input type="text" id="IDCard">
   			</td>
   		</tr>
   		<tr>
   			<td>联系地址:</td>
   			<td>
   				<textarea rows="5" cols="30"></textarea>
   			</td>
   		</tr>
   		<tr>
   			<td align="center" colspan="2">
   			
   				<input type="button" value="提 交" onclick="checkIDCard()">
   				
   			</td>
   		</tr>
   	</table>
   </form>
  </body>
</html>


09

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>小写金额转换为大写金额</title>
<script language="javaScript"> 
	function convert(){
		var money_num = document.getElementById("money_num").value;
		if(money_num==""){
			alert("请输入金额!");
			document.getElementById("money_num").focus();
			return;
		}
		if(isNaN(money_num)){
			alert("请输入数字类型的金额 !");
			return;
		}
		if(money_num>999999999999){
			alert("您输入的金额不能大于999999999999!");
			return;
		}
		//将小数点后保留两位小数
		if(money_num.indexOf(".")>0){
			var decimalStr = money_num.split(".");
			if(decimalStr[1].length>2){
				decimalStr[1]=decimalStr[1].substr(0,2);
			}
			money_num = decimalStr[0]+"."+decimalStr[1];
		}
		value=change(money_num); //调用自定义函数转换
		document.getElementById("money_cn").value=value;  //将转换后的值赋给文本框
	}
	
	function change(str){
		je="零壹贰叁肆伍陆柒捌玖";			//大写的数字(0-9)
		cdw="万仟佰拾亿仟佰拾万仟佰拾元角分";	//金额单位
		var newstring=(str*100).toString();	//将金额值乘以100
		newstringlog=newstring.length;		//乘以100之后的金额的长度
		newdw=cdw.substr(cdw.length-newstringlog);
		num0=0;     	//记录零的个数
		wan=0;     		//记录万位出现的次数
		dxje="";     	//记录大写金额
		for(m=1;m<newstringlog+1;m++){
			xzf=newstring.substr(m-1,1);   
			dzf=je.substr(xzf,1);
			dw=newdw.substr(m-1,1);
			if(dzf=="零"){
				dzf="";
			if(dw=="亿"){
			}else if(dw=="万"){
				dzf="";
				wan=1; 
			}else if(dw=="元"){
 
			}else{
				dw="";	//记录单位		
			}
			num0=num0+1;
			}else{
				if(num0-wan>0){
					if(dw!="角"){
					dzf="零"+dzf;
					}
				}
				num0=0;
 
			}
			dxje=dxje+dzf+dw;
		}
		if(newstring.length!=1){
			if(newstring.substr(newstring.length-2)=="00"){
				dxje=dxje+"整";
			}else{
				dxje=dxje;
			}
		}
		return dxje;
	} 	
</script>
	<style type="text/css">
		table{
			font-size: 20px;
			font-family: 楷体;
			color:navy;
		}
		input{
			font-size: 20px;
			font-family: 楷体;
			color:navy;
		}
		font{
			font-size: 19px;
			font-family: 楷体;
			color:orangered;
		}
	</style>
  </head>
  <body>
   <form action="" id="myform">
   	<table align="center">
   		<tr>
   			<td>请输入小写金额:</td>
   		</tr>	
   		<tr>
   			<td>
   				<input type="text"  id="money_num" size="40"> 			
   			</td>	
   		</tr>  
   		<tr>
   			<td>转换后的大写金额:</td>
   		</tr> 
   		<tr>
   			<td>
				<textarea rows="5" cols="35" id="money_cn"></textarea>
   			</td>
   		</tr> 		
   		<tr>
   			<td align="center">
   				<input type="button" value="转 换" onclick="convert()">
   			</td>
   		</tr>
   	</table>
   </form>
  </body>
</html>


10

<html>
<head>

<title></title>
<link href="CSS/style.css" rel="stylesheet">

</head>

<body>
	<center>
		<form id="form4" name="form4" method="post" action="">
			<label> </label>
			<table width="353" height="140" border="0">
				<tr>
					<td width="104" align="right">用户名:</td>
					<td width="233" align="left"><label for="textfield"></label> <input
						type="text" name="textfield" id="textfield" /></td>
				</tr>
				<tr>
					<td align="right">密码:</td>
					<td align="left"><label for="textfield2"></label> <input
						type="password" name="textfield2" id="textfield2" /></td>
				</tr>
				<tr>
					<td align="right">确认密码:</td>
					<td align="left"><label for="textfield3"></label> <input
						type="password" name="textfield3" id="textfield3" /></td>
				</tr>
				<tr>
					<td colspan="2" align="center"><label> <input
							type="submit" name="Submit" value="提交" onclick="mysubmit()" />
					</label> <label> <input type="reset" name="Submit2" value="重置" />
					</label> <label> <input type="button" name="Submit3" value="关闭"
							onclick="window.close()" />
					</label></td>
				</tr>
			</table>
			<label><br /> </label>
		</form>

	</center>
</body>
</html>
<!--
body{
	FONT-SIZE: 9pt;
	margin-left:0px;
	SCROLLBAR-FACE-COLOR: #0099FF; 
	SCROLLBAR-HIGHLIGHT-COLOR: #ffffff;
	SCROLLBAR-SHADOW-COLOR: #fcfcfc; COLOR: #000000;
	SCROLLBAR-3DLIGHT-COLOR: #ececec;
	SCROLLBAR-ARROW-COLOR: #ffffff;
	SCROLLBAR-TRACK-COLOR: #ececec;
	SCROLLBAR-DARKSHADOW-COLOR: #999966;
	BACKGROUND-COLOR: #fcfcfc

}
a:hover {
	font-size: 9pt;	color: #FF6600;
}
a {
	font-size: 9pt;	text-decoration: none;	color: #676767;
	noline:expression(this.onfocus=this.blur);
}
td{
	font-size: 9pt;	color: #000000;
	padding-left:5px;
}
.btn_grey {
	font-family: "宋体";	font-size: 9pt;color: #333333;	
	background-color: #eeeeee;cursor: hand;padding:1px;height:19px;
	border-top: 1px solid #FFFFFF;border-right:1px solid #666666;
	border-bottom: 1px solid #666666;border-left: 1px solid #FFFFFF;
}
input{
	font-family: "宋体";
	font-size: 9pt;
	color: #333333;
	border: 1px solid #999999;

}
hr{
	border-style:solid;
	height:1px;
	color:#CCCCCC;
}
-->
<meta http-equiv="Content-Type" content="text/html; charset=GBK">

11

<html>
  <head>
    <title>实时显示系统时间</title>
    <script language="javascript">
    function realSysTime(clock){
    	var now=new Date();			//创建Date对象
    	var year=now.getFullYear();	//获取年份
    	var month=now.getMonth();	//获取月份
    	var date=now.getDate();		//获取日期
    	var day=now.getDay();		//获取星期
    	var hour=now.getHours();	//获取小时
    	var minu=now.getMinutes();	//获取分钟
    	var sec=now.getSeconds();	//获取秒钟
    	month=month+1;
    	var arr_week=new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
    	var week=arr_week[day];		//获取中文的星期
    	var time=year+"年"+month+"月"+date+"日 "+week+" "+hour+":"+minu+":"+sec;	//组合系统时间
    	clock.innerHTML="当前时间:"+time;	//显示系统时间
    }
    window.onload=function(){
		window.setInterval("realSysTime(clock)",1000);	//实时获取并显示系统时间
    }
    </script>
  </head>
  
  <body>
    <div id="clock"></div>
  </body>
</html>


正则表达式

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>通过正则表达式验证日期</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script language="javascript"> 
	
	function CheckDate(str){
		var Expression=/^((((1[6-9]|[2-9]\d)\d{2})(\/|\-)(0?[13578]|1[02])(\/|\-)(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})(\/|\-)(0?[13456789]|1[012])(\/|\-)(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})(\/|\-)0?2(\/|\-)(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/; 
		var objExp=new RegExp(Expression);
		if(objExp.test(str)==true){
			return true;
		}else{
			return false;
		}
	}
	
	function check(){
		var mydate = document.getElementById("mydate"); //获得日期文本框对象 
		if(mydate.value==""){							//判断输入的日期是否为空
			alert("请输入日期!");
			mydate.focus();								//使文本框获得焦点
			return;
		}		
		if(!CheckDate(mydate.value)){					//验证日期格式是否正确 
			alert("您输入的日期不正确,请注意日期格式!");
			mydate.focus();
			return;
		}
		document.getElementById("dateForm").submit();	
	}
	</script>
	<style type="text/css">
		table{
			font-size: 13px;
			font-family: 楷体;
			color:navy;
			border: 1px solid;
			border-color: olive;
		}
		input{
			font-size: 13px;
			font-family: 楷体;
			color:navy;
		}
		font{
			font-size: 12px;
			font-family: 楷体;
			color:orangered;
		}
	</style>
  </head>
  
  <body>
   <form action="" id="dateForm">
   	<table>
   		<tr>
   			<td>请输入日期:</td>
   			<td>
   				<input type="text" name="mydate" id="mydate">
   				<font>
   					格式为:2010/06/17或2010-06-17
   				</font>
   			</td>
   		</tr>
   		<tr>
   			<td align="center" colspan="2">
   				<input type="button" value="验 证" onclick="check()">
   				
   			</td>
   		</tr>
   	</table>
   </form>
  </body>
</html>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>验证输入的日期是否正确</title>
<script language="javascript">
	function checkDate(dateStr) {
		var mydate = document.getElementById("mydate");
		if (dateStr == "" || dateStr == null) {
			return false;
		} else {
			if (dateStr.indexOf("-") != -1 || dateStr.indexOf("/") != -1) {
				if (dateStr.indexOf("-") != -1)
					var dateArr = dateStr.split("-");
				if (dateStr.indexOf("/") != -1)
					var dateArr = dateStr.split("/");

				var year = dateArr[0]; //提取年份
				var month = dateArr[1]; //提取月份
				var day = dateArr[2]; //提取日
				//如果年份、月份、日期 不是数字或者<=0,返回false
				if (isNaN(year) || year <= 0) {
					return false;
				}
				if (isNaN(month) || month > 12 || month <= 0) {
					return false;
				}
				if (isNaN(day) || day > 31 || day <= 0) {
					return false;
				}
				//年份能被4整除并且不能被100整除,或者能被400整除,则为闰年
				if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
					if (month == 2) { //闰年的2月 
						if (day > 29) {
							return false;
						}
					}
				} else { //不是闰年的2月 
					if (month == 2) {
						if (day > 28) {
							return false;
						}
					}
				}
				//1、3、5、7、8、10、12月份为31天 
				var m1 = new Array(1, 3, 5, 7, 8, 10, 12);
				for (var i = 0; i < m1.length; i++) {
					if (parseInt(month) == m1[i]) {
						if (day > 31) {
							return false;
						}
					}
				}
				//4、6、9、12月份为31天 
				var m2 = new Array(4, 6, 9, 11);
				for (var j = 0; j < m2.length; j++) {
					if (parseInt(month) == m2[j]) {
						if (day > 30) {
							return false;
						}
					}
				}
			} else {
				return false;
			}
		}
		return true;
	}

	function check() {
		var mydate = document.getElementById("mydate");
		if (checkDate(mydate.value)) {
			document.getElementById("dateForm").submit();
		} else {
			if (mydate.value == "" || mydate.value == null) {
				alert("请输入日期!");
				mydate.focus(); //日期文本框获得焦点
				return;
			}
			mydate.focus();
			alert("您输入的日期格式不正确!");
		}
	}
</script>
<style type="text/css">
table {
	font-size: 13px;
	font-family: 楷体;
	color: navy;
	border: 1px solid;
	border-color: olive;
}

input {
	font-size: 13px;
	font-family: 楷体;
	color: navy;
}

font {
	font-size: 12px;
	font-family: 楷体;
	color: orangered;
}
</style>
</head>
<body>
	<form action="" id="dateForm">
		<table>
			<tr>
				<td>请输入日期:</td>
				<td><input type="text" name="mydate" id="mydate"> <font>
						格式为:2010/06/17或2010-06-17 </font></td>
			</tr>
			<tr>
				<td align="center" colspan="2"><input type="button" value="验 证"
					onclick="check()"></td>
			</tr>
		</table>
	</form>
</body>
</html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>检查表单元素的值是否为空</title>
<script language="javascript"> 
	
	function check(){
		var myform = document.getElementById("myform");		//获得form表单对象
		for(var i=0;i<myform.length;i++){		//循环form表单
			if(myform.elements[i].value==""){	//判断每一个元素是否为空
				alert(myform.elements[i].title+"不能为空!");
				myform.elements[i].focus();		//元素获得焦点
				return ;
			}
		}
		myform.submit();
	}
	</script>
<style type="text/css">
table {
	font-size: 13px;
	font-family: 楷体;
	color: navy;
}

input {
	font-size: 13px;
	font-family: 楷体;
	color: navy;
}

font {
	font-size: 12px;
	font-family: 楷体;
	color: orangered;
}

.style1 {
	width: 500;
	height: 300;
}
</style>
</head>

<body>
	<fieldset class="style1">
		<legend>留言薄</legend>

		<form action="" id="myform">
			<table align="center">
				<tr>
					<td>留言人:</td>
					<td><input type="text" name="messageUser" id="messageUser"
						title="留言人"></td>
				</tr>
				<tr>
					<td>留言标题:</td>
					<td><input type="text" name="messageTitle" id="messageTitle"
						title="留言标题"></td>
				</tr>
				<tr>
					<td>留言内容:</td>
					<td><textarea rows="8" cols="45" id="messageContent"
							title="留言内容"></textarea></td>
				</tr>
				<tr>
					<td align="center" colspan="2"><input type="button"
						value="提 交" onclick="check()"></td>
				</tr>
			</table>

		</form>
	</fieldset>
</body>
</html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>验证是否为数字</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script language="javascript"> 
	function check(){
		var age = document.getElementById("age");
		if(age.value==null||age.value==""){
			alert("请输入年龄!");
			age.focus();
			return;
		}
		if(isNaN(age.value)){
			alert("年龄必须为数字!");
			age.focus();
			return;
		}
		document.getElementById("myform").submit();	

	}
	</script>
	<style type="text/css">
		table{
			font-size: 13px;
			font-family: 楷体;
			color:navy;
		
	
		}
		input{
			font-size: 13px;
			font-family: 楷体;
			color:navy;
		}
		font{
			font-size: 12px;
			font-family: 楷体;
			color:orangered;
		}
		.style1{
			width: 500;
			height: 260;
		}
	</style>
  </head>
  
  <body>
    <fieldset class="style1"><legend>用户注册</legend>
    
   <form action="" id="myform">
   	<table align="center">
   		<tr>
   			<td>用户名:</td>
   			<td>
   				<input type="text"  id="name"> 			
   			</td>
   		</tr>
   		<tr>
   			<td>密码:</td>
   			<td>
   				<input type="password"  id="pwd"> 			
   			</td>
   		</tr>
   		<tr>
   			<td>确认密码:</td>
   			<td>
   				<input type="password"  id="pwd1"> 			
   			</td>
   		</tr>
   		<tr>
   			<td> 性别:</td>
   			<td>
   				<input type="radio" name="sex" id="man" value="m" />男	
   				<input type="radio" name="sex" id="woman" value="f" />女			
   			</td>
   		</tr>
   		
   		<tr>
   			<td>年龄:</td>
   			<td>
   				<input type="text" id="age">
   			</td>
   		</tr>
   		<tr>
   			<td align="center" colspan="2">
   				<input type="button" value="提 交" onclick="check()">
   			</td>
   		</tr>
   	</table>
   	
   </form>
   </fieldset>
  </body>
</html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>验证E-mail是否正确</title>
	<script language="javascript"> 
	function checkEmail(){
		var email = document.getElementById("email");
		if(email.value==null||email.value==""){//判断文本框是否为空 
			alert("请输入E-mail地址!");
			email.focus();
			return;
		}
		var regExpression = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
		var objExp = new RegExp(regExpression);	//创建正则表达式对象 
		if(objExp.test(email.value)==false){	//通过 test()函数测试字符串是否与表达式的模式匹配 
			alert("您输入的E-mail地址不正确!");
			email.focus();						//使文本框获得焦点 
			return;								
		}
		document.getElementById("myform").submit();
	}
	</script>
	<style type="text/css">
		table{
			font-size: 13px;
			font-family: 楷体;
			color:navy;
		
	
		}
		input{
			font-size: 13px;
			font-family: 楷体;
			color:navy;
		}
		font{
			font-size: 12px;
			font-family: 楷体;
			color:orangered;
		}
		.style1{
			width: 500;
			height: 260;
		}
	</style>
  </head>
  
  <body>
    <fieldset class="style1"><legend>用户注册</legend>
   <form action="" id="myform">
   	<table align="center">
   		<tr>
   			<td>用户名:</td>
   			<td>
   				<input type="text"  id="name"> 			
   			</td>
   		</tr>
   		<tr>
   			<td>密码:</td>
   			<td>
   				<input type="password"  id="pwd"> 			
   			</td>
   		</tr>
   		<tr>
   			<td>确认密码:</td>
   			<td>
   				<input type="password"  id="pwd1"> 			
   			</td>
   		</tr>
   		<tr>
   			<td> 性别:</td>
   			<td>
   				<input type="radio" name="sex" id="man" value="m" />男	
   				<input type="radio" name="sex" id="woman" value="f" />女			
   			</td>
   		</tr>
   		
   		<tr>
   			<td>年龄:</td>
   			<td>
   				<input type="text" id="age">
   			</td>
   		</tr>
   		<tr>
   			<td>密码提示问题:</td>
   			<td>
   				<select name="question">
   					<option>请选择</option>
   					<option>你的父亲叫什么名字?</option>
   					<option>你的小学老师的名字?</option>
   					<option>你的生日?</option>
   					<option>你的初中学校的名字?</option>
   					<option>你喜欢什么颜色?</option>
   					<option>你喜欢吃的水果是?</option>
   				</select>
   			</td>
   		</tr>
   		<tr>
   			<td>密码提示答案:</td>
   			<td>
   				
   				<input type="text" id="answer">
   			</td>
   		</tr>
   		<tr>
   			<td>E-mail:</td>
   			<td>
   				<input type="text" id="email">
   			</td>
   		</tr>
   		<tr>
   			<td align="center" colspan="2">
   				<input type="button" value="提 交" onclick="checkEmail()">
   				
   			</td>
   		</tr>
   	</table>
   </form>
   </fieldset>
  </body>
</html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>验证手机号码是否正确</title>
	<script language="javascript"> 
	function checkPhone(){
		var mobileNo = document.getElementById("mobileNo");
		var regExpression = /^(86)?((13\d{9})|(15[0,1,2,3,5,6,7,8,9]\d{8})|(18[0,5,6,7,8,9]\d{8}))$/;
		if(!regExpression.test(mobileNo.value)){	
			alert("您输入的手机号码有误!");
			mobileNo.focus();						
			return;								
		}
			document.getElementById("myform").submit();
	}
	</script>
	<style type="text/css">
		table{
			font-size: 13px;
			font-family: 楷体;
			color:navy;
		}
		input{
			font-size: 13px;
			font-family: 楷体;
			color:navy;
		}
		font{
			font-size: 12px;
			font-family: 楷体;
			color:orangered;
		}
		.style1{
			width: 500;
			height: 260;
		}
	</style>
  </head>
  
  <body>
    <fieldset class="style1"><legend>添加通讯信息</legend>
    
   <form action="" id="myform">
   	<table align="center">
   		<tr>
   			<td>姓名:</td>
   			<td>
   				<input type="text"  id="name"> 			
   			</td>
   		</tr>	
   		<tr>
   			<td> 性别:</td>
   			<td>
   				<input type="radio" name="sex" id="man" value="m" />男	
   				<input type="radio" name="sex" id="woman" value="f" />女			
   			</td>
   		</tr>
   		
   		<tr>
   			<td>年龄:</td>
   			<td>
   				<input type="text" id="age">
   			</td>
   		</tr>
   		<tr>
   			<td>电子邮箱:</td>
   			<td>
   				<input type="text" id="email">
   			</td>
   		</tr>
   		<tr>
   			<td>手机号码:</td>
   			<td>
   				<input type="text" id="mobileNo">
   			</td>
   		</tr>
   		<tr>
   			<td>联系地址:</td>
   			<td>
   				<textarea rows="5" cols="30"></textarea>
   			</td>
   		</tr>
   		<tr>
   			<td align="center" colspan="2">
   				<input type="button" value="提 交" onclick="checkPhone()">
   			</td>
   		</tr>
   	</table>
   	
   </form>
   </fieldset>
  </body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值