以下代码加入html的<body>区:
<script language="javascript">
<!-- begin
function chk(email, formname)
{
invalid = "";
if (!email)
invalid = "请输入您的email地址。";
else {
if ( (email.indexof("@") == -1) || (email.indexof(".") == -1) )
invalid += "\n\nemail地址不合法。应当包含'@'和'.';例如('.com')。请检查后再递交。";
if (email.indexof("your email here") > -1)
invalid += "\n\nemail地址不合法,请检测您的email地址,在域名内应当包含'@'和'.';例如('.com')。";
if (email.indexof("\\") > -1)
invalid += "\n\nemail地址不合法,含有非法字符(\\)。";
if (email.indexof("/") > -1)
invalid += "\n\nemail地址不合法,含有非法字符(/)。";
if (email.indexof("'") > -1)
invalid += "\n\nemail地址不合法,含有非法字符(')。";
if (email.indexof("!") > -1)
invalid += "\n\nemail地址不合法,含有非法字符(!)。";
if ( (email.indexof(",") > -1) || (email.indexof(";") > -1) )
invalid += "\n\n只输入一个email地址,不要含有分号和逗号。";
if (email.indexof("?subject") > -1)
invalid += "\n\n不要加入'?subject=...'。";
}
if (invalid == "")
{
return true;
}
else
{
alert("输入的email可能包含错误:" + invalid);
return false;
}
}
// end -->
</script>
<form method="post" name="myform" action="submit.asp" onsubmit="return chk(document.myform.email.value)">
<div align="center">
<input type="text" name="email" value="your email here">
<input type="submit" name="submit" value="submit">
</div>
</form>
<script language="javascript">
<!--//
function validate(){
var invalid = " "; //非法字符是空格你可以改为其它字符或数字
if (document.submitform.filename.value.indexof(invalid) > -1) {
alert("文件名不能含有空格!");
return false;
}
else {
return true;
}
}
// -->
</script>
<form name=submitform onsubmit="return validate()">
<input type=file name=filename>
<p>
<input type=submit value="submit">
</form>
表单递交合法性检测-日期
<html>
<head>
<script language="javascript">
<!--
function testkey(e){
chars= "0123456789/";
e = window.event;
if(chars.indexof(string.fromcharcode(e.keycode))==-1) window.event.keycode=0;
};
function valdate(m, d, y){
months= new array(31,28,31,30,31,30,31,31,30,31,30,31);
leap = false;
if((y % 4 == 0) && ((y % 100 != 0) || (y %400 == 0)))
leap = true;
if((d < 1) || (d > 31) || (m < 1) || (m > 12) || (y < 0))
return(false);
if((d > months[m-1]) && !((m == 2) && (d > 28)))
return(false);
if(!(leap) && (m == 2) && (d > 28))
return(false);
if((leap) && (m == 2) && (d > 29))
return(false);
};
function formatdate(dateform){
cdate = dateform.value;
dsize = cdate.length;
scount= 0;
if (document.form1.date.value == ""){
alert("请输入日期!");
return false ;
}
if(cdate=='') return;
for(var i=0; i < dsize; i++)
(cdate.substr(i,1) == "/") ? scount++ : scount;
if (scount != 2){
alert("输入的日期格式必须是\n ''月/日/年''");
dateform.select();
return(false);
};
//检测输入的年份是2位数还是4位数;
ysize = cdate.substring(cdate.lastindexof("/")+1,dsize).length
if(ysize<2 || ysize>4 || ysize == 3){
alert('您输入的日期错误 !');
dateform.select();
return false;
};
//将输入的日期字符串分隔成3部分 (month, day & year)
idxbari = cdate.indexof("/");
idxbarii= cdate.lastindexof("/");
strm = cdate.substring(0,idxbari);
strd = cdate.substring(idxbari+1,idxbarii);
stry = cdate.substring(idxbarii+1,dsize);
strm = (strm.length < 2 ? '0'+strm : strm);
strd = (strd.length < 2 ? '0'+strd : strd);
if(stry.length == 2)
stry = (stry > 50 ? '19'+stry : '20'+stry);
dateform.value = strm+'/'+strd+'/'+stry;
ok = valdate(strm, strd, stry);
if(ok==false){
alert("您输入的日期错误 !");
dateform.select();
return false;
};
};
-->
</script>
<title>日期合法性检测</title>
</head>
<body onload="javascript:document.form1.date.focus()" bgcolor="#ffffff">
<form name="form1" method="post" onsubmit="return testkey(event)" action="">
输入正确的日期(月/日/年):
<input type=text maxlength =10 name="date" size=10 onblur="formatdate(this)" value="">
</form>
</body>
</html>
说明:此脚本的用途是比较全面地检测输入日期的合法性,除了做非空检测外,还有效地检测了不同年月日期的合法性问题。比如在不是闰年的2月输入了29日等。黄色代码与脚本的检测无关,作用是页面读出页面后光标停留在日期文本框内。可以不要。
注意:(1)<form>标签中表单的名字form1和日期文本框的名字data(加重字体)与脚本是有关的,也就是说你如果改动了它们的名字,凡是在脚本中引用from1和data的部分都要修改。切切!!!
(2)javascript是大小写敏感的,所以注意大小写的区别和一致性原则。
(3)此脚本应该与cgi/asp等服务器端的递交处理程序配合使用,用于客户端的合法性检测。本例没有将submit按钮作上去,你所处理的表单中可能包括更多的内容。
这里仅仅提供了一个脚本思路,你不一顶非要全部照搬脚本,可以仅仅取脚本的一部分使用(主要是算法)。
如下代码加入html的<head>区:
<script language="javascript">
function numericcheck(){
nr1=document.myform.content.value;
flg=0;
str="";
spc=""
arw="";
for (var i=0;i<nr1.length;i++){
cmp="0123456789"
tst=nr1.substring(i,i+1)
if (cmp.indexof(tst)<0){
flg++;
str+=" "+tst;
spc+=tst;
arw+="^";
}
else{arw+="_";}
}
if (flg!=0){
if (spc.indexof(" ")>-1) {
str+="和空格";
}
alert("这里只接受数字,发现"
+flg+"个不可接受的字符:\n"+str);
return false;
}
}
</script>
如下代码加入html的<body>区,表单递交部分:
<form name="myform" method="post" enctype="text/plain" onsubmit="return numericcheck()" action="mailto:mail@mail.com">
<input type="text" name="content">
<input type="submit" name="submit" value="递交">
</form>
注意:表单的名字和文本框的名字要保持前后一样。
表单递交合法性检测-只允许填写字母
<script language="javascript">
<!--
function checkform()
{
if (document.form.yourname.value == "" )
{
alert ("请输入姓名!");
return false;
}
var letters = "abcdefghijklmnopqrstuvwxyz"
for (i=0; i < document.form.yourname.value.length; i++)
{
var checkchar = document.form.yourname.value.charat(i);
checkchar = checkchar.touppercase();
if (letters.indexof(checkchar) == -1)
{
alert ("请输入英文姓名!");
return false;
}
}
}
// -->
</script>
<form action="" name="form" method="post" onsubmit="return checkform()">
<div align="center">
<input type="text" name="yourname">
<input type="submit" name="submit" value="submit">
</div>
</form>
表单递交内容检测脚本-这里只接受数字
如下代码加入html的<head>区:
<script language="javascript">
function numericcheck(){
nr1=document.myform.content.value;
flg=0;
str="";
spc=""
arw="";
for (var i=0;i<nr1.length;i++){
cmp="0123456789"
tst=nr1.substring(i,i+1)
if (cmp.indexof(tst)<0){
flg++;
str+=" "+tst;
spc+=tst;
arw+="^";
}
else{arw+="_";}
}
if (flg!=0){
if (spc.indexof(" ")>-1) {
str+="和空格";
}
alert("这里只接受数字,发现"
+flg+"个不可接受的字符:\n"+str);
return false;
}
}
</script>
如下代码加入html的<body>区,表单递交部分:
<form name="myform" method="post" enctype="text/plain" onsubmit="return numericcheck()" action="mailto:mail@mail.com">
<input type="text" name="content">
<input type="submit" name="submit" value="递交">
</form>
注意:表单的名字和文本框的名字要保持前后一样。