javascript - 表单上不可见的提交按钮

标签 javascript html forms submit show

我有一个受验证代码约束的表单。 前任。用户名必须包含字母字符。 因此返回 true 或 false。

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 标记之后起作用

最佳答案

试试这个

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

关于javascript - 表单上不可见的提交按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10071451/

相关文章:

javascript - 如何正确执行服务器端 api 请求?

jquery - 如何从管理面板页面显示单击开/关 SVG 图标并在不同页面上显示结果

javascript - php - 选择选项值未传递到帖子

php - 如何在发布请求后保持选择表单项?

javascript - 有没有办法让 Firefox 和 Chrome 滚动相同的元素?

javascript - 删除按钮上的确认对话框

javascript - Jasmine测试私有(private)回调函数

按列呈现的 HTML 表格

javascript - 当我使用 input type=button 时,它的值不会发送到服务器

html - 如何在输入框处于事件状态时显示文本