<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>

解决方案 »

  1.   

    <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>
      

  2.   

    <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>
      

  3.   

    <html>
    <script language="javascript">
    function CheckValue(name,desc){
    if(name.value==""){
    alert(desc+"&sup2;&raquo;&Auml;&Uuml;&Icirc;&ordf;&iquest;&Otilde;");
    name.focus();
    return false;
    }
    }
    function CheckForm(theForm){
      
    if(!CheckValue(theForm.pwd,"&Atilde;&Uuml;&Acirc;&euml;"))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>
      

  4.   

    可以这样做:
    以下是用于验证文本框内容的代码(我只列了一部分),你可以将其单独作为一个文件,在要用到页面将其包含进去
    <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>
      

  5.   

    To bencalie(Bencalie) 
    你的方法可以,再有个问题就是我在
    function CheckValue(theForm,name,desc){}
    这里送入一个theform,这个是form名称,我不能写死,因为是公共用的,所以我想请问这个theform名称是否可以传过来,怎么用呢
      

  6.   

    我觉得你写程序,对象的概念不强。function CheckForm(theForm,aaa){
      
    if(!CheckValue(theForm,pwd,"密码"))return false;
    return false;
    }条件当中的pwd是个什么东西?在这里它什么都不是,字符串都不是。
      

  7.   

    是我错了,目前我按照bencalie(Bencalie)的方法做了,是可以,不过form的名字该如何传递呢,请各位再次指教,谢谢教导
      

  8.   

    <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呢
      

  9.   

    function CheckValue(theForm,name,desc){
    if(eval("theForm."+name).value==""){
    alert(desc+"不能为空");
    eval("theForm."+name).focus();
    return false;
    }
    else
    return true}
      

  10.   

    bencalie(Bencalie) ,
    我试过了,你的if(eval("theForm."+name).value==""){}
    有问题,语法错误,是这样我的form名字是传进来的,这个CheckValue可能会给多个表单使用,所以名字会不同,你改成这样提示错误了,你看哪错了呢,谢谢,其他人回答对也给分
      

  11.   

    <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>
      

  12.   

    非常感谢各位,结贴,尤其感谢:bencalie(Bencalie)