在javascript这么做不行吗?怎么办,在线等,送分

jamsband 2002-11-06 10:07:54
可以建立一个html文件名字为test.html
<html>
<script language="javascript">
function CheckValue(theForm,name,desc){
if(theForm.name.value==""){
alert(desc+"不能为空");
theForm.name.setfocus;
return false;
}
}
function CheckForm(theForm,aaa){

if(!CheckValue(theForm,pwd,"密码"))return false;
return false;
}
</script>
<body>
<form name="post" action="test.html" onsubmit="javascript:return CheckForm(this,1);">
<input type="text" name="pwd" value="">
<input name="Submit" type="submit" value="Submit">
</form>
</body>
</html>
我知道我写的不大对,可是应该怎么写呢,就是要实现把判断封装成一个函数,很急,只要用这种方法不填写pwd则弹出提示就可以了,谢谢
...全文
46 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
jamsband 2002-11-06
  • 打赏
  • 举报
回复
非常感谢各位,结贴,尤其感谢:bencalie(Bencalie)
bencalie 2002-11-06
  • 打赏
  • 举报
回复
<html>
<script language="javascript">
function CheckValue(theForm,name,desc){
if(eval("theForm."+name).value==""){
alert(desc+"不能为空");
eval("theForm."+name).focus();
return false;
}
else
return true

}
function CheckForm(theForm,aaa){

if(!CheckValue(theForm,'pwd',"密码"))return false;
else
return true;
}
</script>
<body>
<form name="post" action="test.html" onsubmit="javascript:return CheckForm(this,1);">
<input type="text" name="pwd" value="">
<input name="Submit" type="submit" value="Submit">
</form>
</body>
</html>
jamsband 2002-11-06
  • 打赏
  • 举报
回复
bencalie(Bencalie) ,
我试过了,你的if(eval("theForm."+name).value==""){}
有问题,语法错误,是这样我的form名字是传进来的,这个CheckValue可能会给多个表单使用,所以名字会不同,你改成这样提示错误了,你看哪错了呢,谢谢,其他人回答对也给分
bencalie 2002-11-06
  • 打赏
  • 举报
回复
function CheckValue(theForm,name,desc){
if(eval("theForm."+name).value==""){
alert(desc+"不能为空");
eval("theForm."+name).focus();
return false;
}
else
return true

}
jamsband 2002-11-06
  • 打赏
  • 举报
回复
<script language="javascript">
function CheckValue(theForm,name,desc){
if(eval("document.all."+name).value==""){
alert(desc+"不能为空");
eval("document.all."+name).focus();
return false;
}
else
return true

}
function CheckForm(theForm,aaa){

if(!CheckValue(theForm,'pwd',"密码"))return false;
else
return true;
}
</script>
请问这种方式的话我如何把theForm传递到CheckValue中,替换掉document.all中的all呢
jamsband 2002-11-06
  • 打赏
  • 举报
回复
是我错了,目前我按照bencalie(Bencalie)的方法做了,是可以,不过form的名字该如何传递呢,请各位再次指教,谢谢教导
blues-star 2002-11-06
  • 打赏
  • 举报
回复
我觉得你写程序,对象的概念不强。

function CheckForm(theForm,aaa){

if(!CheckValue(theForm,pwd,"密码"))return false;
return false;
}

条件当中的pwd是个什么东西?在这里它什么都不是,字符串都不是。
jamsband 2002-11-06
  • 打赏
  • 举报
回复
To bencalie(Bencalie)
你的方法可以,再有个问题就是我在
function CheckValue(theForm,name,desc){}
这里送入一个theform,这个是form名称,我不能写死,因为是公共用的,所以我想请问这个theform名称是否可以传过来,怎么用呢
freefalcon 2002-11-06
  • 打赏
  • 举报
回复
可以这样做:
以下是用于验证文本框内容的代码(我只列了一部分),你可以将其单独作为一个文件,在要用到页面将其包含进去
<script language=javascript>
function isNotEmpty(obj)
{
if(arguments.length==1)msg="必填项目不能为空!";
else msg=arguments[1];
if(obj.value=="")
{
alert(msg);
obj.focus();
return false;
}
else return true;
}
function isEmail(obj)
{
if(arguments==1)msg="Email格式不正确!";
else msg=arguments[1];
var re=/^\w+@(\w+\.)+\w+$/;
if (!re.test(obj.value))
{
alert(msg);
obj.select();
return false;
}
else return true;
}
</script>
下面是具体对某个表单的验证:
<script>
function checkForm1(){
if(isNotEmpty(form1.user,"请输入用户姓名!")
&&isNotEmpty(form1.email,"请输入用户邮件!")
&&isEmail(form1.email,"邮件格式不正确!"))
return true;
else return false;
}
</script>
<form name="form1" action="" onsubmit="return checkForm1()">
用户姓名<input name="user"><br>
电子邮件<input name="email">
</form>
blues-star 2002-11-06
  • 打赏
  • 举报
回复
<html>
<script language="javascript">
function CheckValue(name,desc){
if(name.value==""){
alert(desc+"²»ÄÜΪ¿Õ");
name.focus();
return false;
}
}
function CheckForm(theForm){

if(!CheckValue(theForm.pwd,"ÃÜÂë"))return false;
return false;
}
</script>
<body>
<form name="post" action="test.html" onsubmit="return CheckForm(this,1);">
<input type="text" name="pwd" value="">
<input name="Submit" type="submit" value="Submit">
</form>
</body>
</html>
meizz 2002-11-06
  • 打赏
  • 举报
回复
<form name=form1 onsubmit="return cc()">
<input type=password name=pass><br>
<input type=submit value=submit></form>

<script language=javascript>
function String.prototype.Trim() {return this.replace(/(^\s*)|(\s*$)/g,"");}
function cc()
{
var e = document.form1.pass;
if(e.value.Trim()=="")
{
alert("密码栏不许为空!");
e.value = ""; //消除用户输入空格的问题
e.focus();
return false;
}
return true;
}
</script>
bencalie 2002-11-06
  • 打赏
  • 举报
回复
<html>
<script language="javascript">
function CheckValue(theForm,name,desc){
if(eval("document.all."+name).value==""){
alert(desc+"不能为空");
eval("document.all."+name).focus();
return false;
}
else
return true

}
function CheckForm(theForm,aaa){

if(!CheckValue(theForm,'pwd',"密码"))return false;
else
return true;
}
</script>
<body>
<form name="post" action="test.html" onsubmit="javascript:return CheckForm(this,1);">
<input type="text" name="pwd" value="">
<input name="Submit" type="submit" value="Submit">
</form>
</body>
</html>
无爱大叔 2002-11-06
  • 打赏
  • 举报
回复
<html>
<script language="javascript">
function CheckValue(){
if(theForm.pwd.value=="")
{
alert("Password required!");
theForm.pwd.focus();
return false;
}
}
</script>
<body>
<form name="theForm" action="test.html" onsubmit="return CheckValue();">
<input type="text" name="pwd" value="">
<input name="Submit" type="submit" value="Submit">
</form>
</body>
</html>

87,775

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧