js的表单验证问题
发布网友
发布时间:2022-05-01 10:24
我来回答
共3个回答
热心网友
时间:2022-04-23 03:41
<html>
<head>
<base href="<%=basePath%>">
<script type="text/javascript">
function check()
{
if(document.enquiry.name.value=="")
{
alert("please fill in your name");
enquiry.name.focus();
return false;
}
if(document.enquiry.email.value=="")
{
alert("please fill in your email");
enquiry.email.focus();
return false;
}
var patrn=/^\w+@\w+\.((com)|(cn)|(com\.cn)|(net))$/;
var s=document.enquiry.email.value;
if(!patrn.exec(s)){
alert("please correct your email");
enquiry.email.focus();
return false;
}
if(document.enquiry.en.value=="")
{
alert("please fill in your enquiry");
enquiry.en.focus();
return false;
}
}
</script>
<title>javascript</title>
</head>
<body>
<form name="enquiry" action="sub.asp" method="post">
姓名:<input name="name" type="text" class="textfield" id="name" size="18"/> <br/>
邮箱:<input name="email" type="text" class="textfield" id="email" size="18"/> <br/>
询问:<textarea name="en" cols="85" rows="6" class="textfield" id="en"></textarea> <br/>
<input type="submit" value="提交" onclick="return check();">
</form>
</body>
</html>
非常精确的电子邮箱验证正则表达式....
已经正常运行并调试....
楼主可运行试试看...并且添加了输入错误后光标自动回到输入框中...
还在...心想楼主问这个问题我想楼主的正则表达式一定有所欠缺...
我还是把我学正则表达式的网址留给楼主把....楼主可以去看一下...
当然学一下更好...
http://www.javaeye.com/topic/30728
如果有什么不懂的可以百度HI问我...帮你解决...
祝楼主早日成功!!!!
热心网友
时间:2022-04-23 04:59
<html>
<body>
<script language="javascript">
function check()
{
if(document.enquiry.name.value=="")
{
alert("please fill in your name");
return false;
}
if(document.enquiry.email.value=="")
{
alert("please fill in your email");
return false;
}
if(!/^\w+[@]\w+[.]\w+$/.test(document.enquiry.email.value))
// /^\w+[@]\w+[.]\w+$/ 用该正则来匹配你输入的document.enquiry.email.value的值,如果匹配就返回true,否则false
// 前面加了个!表示如果不匹配就进入if.
{
alert("please correct your email");
return false;
}
if(document.enquiry.enquiry.value=="")
{
alert("please fill in your enquiry");
return false;
}
}
</script>
<form name="enquiry" onsubmit="return check()" action="sub.asp" method="post">
<input name="name" type="text" class="textfield" id="name" size="18">
<input name="email" type="text" class="textfield" id="email" size="18">
<textarea name="enquiry" cols="85" rows="6" class="textfield" id="enquiry"></textarea>
<input type="submit" value="提交"/>
</form>
</body>
</html>
热心网友
时间:2022-04-23 06:33
你到底想做什么?