1

我有一个受验证代码约束的表单。前任。userName 必须有字母字符。因此返回真或假。

function isAlphapet()
{
  var alphaExp = /^[a-zA-Z]+$/;
  var namee=document.validation.userName.value;
  var nalt=document.getElementById('name1');
  if(namee!="")
   {
      if(!namee.match(alphaExp))
      {
       nalt.innerHTML="<font color='red'> Invalid Name: " + document.validation.userName.value + "</font>";
       document.validation.userName.focus();
       document.validation.userName.value="";
       //This will remove the success class if the user tries to modify it incorrectly.
       document.getElementById("userName").className = document.getElementById("userName").className.replace(" validationSuccess", "");
       //This is calling the css class name validationError to color the text box border in red if there is an error.
       //Leaving the replacing attribute empty incase the user hasn't input the correct string.
       document.getElementById("userName").className = document.getElementById("userName").className + " validationError";
         return false;  
       }else{//if it's validated correctly
          nalt.innerHTML="";
         //The text box should be green.
          document.getElementById("userName").className = document.getElementById("userName").className + " validationSuccess";
            return true; 
        }
   }
  else  if(namee.length==0) { //if the user leaves it blank.
   nalt.innerHTML="<font color='red'> Enter Name</font>";
    document.getElementById('name1').focus();
    document.getElementById("userName").className = document.getElementById("userName").className.replace(" validationSuccess", "");
    document.getElementById("userName").className = document.getElementById("userName").className + " validationError";
    //The above is explained on line 27+.
   return false;
    }
}

在用户名文本框的表单中,我有:

Your Name:<br /><input name="userName" id="userName" onBlur="isAlphapet()" type="text" size="20" maxlength="25" /><br />

一旦以上返回true,我需要启用我的按钮。但是由于某种原因,当我尝试在正文中的 JScript 中使用它时,isAlphapet 没有返回任何内容。

有没有人有办法解决吗?

非常感激。

编辑

感谢您回复斯里尼。

唔。不,在我进行更改后,整个验证功能都停止了工作。我想要达到的目标比听起来要简单一些。我需要一个 if 语句或类似以下内容:

if (isAlphapet()== true && emailvalid()== true && browserValid()==true){
    document.validation.sumb.disabled=false;
}
else{
    document.validation.subm.diabled=true;
}

验证是我的表单的名称。我尝试在标题和正文中使用它。它也不识别我的函数的返回值。但是按钮禁用和启用仅在 /form 标记之后有效

4

1 回答 1

0

试试这个

              <html>
            <head>
            <script type="text/javascript">
            function isAlphapet()
            {

             var alphaExp = /^[a-zA-Z]+$/;
             var namee=document.getElementById('userName').value;

             var nalt=document.getElementById('name1');
      if(namee!="")
      {
    if(!namee.match(alphaExp))
    {
        alert(namee);       


        nalt.innerHTML="<span style='color:red;'> Invalid Name: " + namee + "</span>";
        document.validation.userName.focus();
        document.validation.userName.value="";
   //This will remove the success class if the user tries to modify it incorrectly.
         document.getElementById("userName").className = document.getElementById("userName").className.replace("             validationSuccess",        "");
   //This is calling the css class name validationError to color the text box border in red if there is an error.
   //Leaving the replacing attribute empty incase the user hasn't input the correct string.
         document.getElementById("userName").className = document.getElementById("userName").className + " validationError";
         return false;  
      }

      else{//if it's validated correctly
      nalt.innerHTML="";
     //The text box should be green.
      document.getElementById("userName").className = document.getElementById("userName").className + " validationSuccess";
        return true; 
      }
  }
    else  if(namee.length==0) { //if the user leaves it blank.
       nalt.innerHTML="<font color='red'> Enter Name</font>";
       document.getElementById('name1').focus();
       document.getElementById("userName").className = document.getElementById("userName").className.replace(" validationSuccess", "");
       document.getElementById("userName").className = document.getElementById("userName").className + " validationError";
//The above is explained on line 27+.
      return false;
    }
 }

            </script>
            </head>
        <body>
         <div style="float:left">
            <form>
                Your Name:<br /><input name="userName" id="userName" onBlur="isAlphapet()" type="text" size="20" maxlength=                    "25" /><br />
            </form>
        </div>
          <div id="name1" style="float:left"></div>
         </body>
        </html> 
于 2012-04-09T10:43:18.477 回答