在网页制作中,有很多需要用户输入的地方,这样就需要验证用户输入是否正确,验证可以在前台使用
js进行,也可以在后台进行.示例为使用js进行文本框输入是否为空的验证
1.将下面的代码加在<head></head>之间
js代码 :
<script type="text/javascript">
<!--
function check(theform)
{
if(theform.username.value=="")
{
alert("username can’t null");
theform.username.focus();
return false;
}
if(theform.password.value=="")
{
alert("password can’t null");
theform.password.focus();
return false;
}
return true;
}
-->
</script>
表单中的两个文本框分别命名为:username和password
2.在form标签中加入当提交表单事检查代码如下
片段:
<form action="/hello" onsubmit="return check(this)">
js进行,也可以在后台进行.示例为使用js进行文本框输入是否为空的验证
1.将下面的代码加在<head></head>之间
js代码 :
<script type="text/javascript">
<!--
function check(theform)
{
if(theform.username.value=="")
{
alert("username can’t null");
theform.username.focus();
return false;
}
if(theform.password.value=="")
{
alert("password can’t null");
theform.password.focus();
return false;
}
return true;
}
-->
</script>
表单中的两个文本框分别命名为:username和password
2.在form标签中加入当提交表单事检查代码如下
片段:
<form action="/hello" onsubmit="return check(this)">