如何用javascript控制密码字段

时间:2013-11-06 17:40:41

标签: javascript

我正在使用javascript制作网站表单,我正在尝试控制密码字段。我正在使用这个jscript函数

 function passcheck() {
    var pass=document.getElementById('password');
    if (pass.value.length<=6)
    {alert("The password must me greater the");


    }

   }

和表格“:

<input type="password" name="password1"  onchange="passcheck()">

但它不起作用。 我能做什么?请帮帮我

我解决了。不,我的功能像这样lokk

   function passcheck() {
    var pass=document.getElementById('password1');
      if (pass.value.length<=6)
    {alert("The password must me greater the"); 

    }
      }

但是我想在警报中显示消息,但我想在输入的右边显示它。我该怎么办?

现在我想在密码少于6个字符时取消按钮。我已经完成了这个功能

function passcheck() {
    var pass=document.getElementById('password1');
    var sb=document.getElementById('submit');
      if (pass.value.length<=6)
    {document.getElementById('error').style.display = 'block';
    sb.disable=true;}
     else
      {
        sb.disable=false;
       }

和html部分是这个

 <input type="password" name="password1" id="password1"  class="textinput" onchange="passcheck()"><br>
    <input type="button" value="Submit" id="submit" onclick="location.href='userlogin.html'" class="button" ><br>

但按钮仍然启用...我该怎么办?

            }

2 个答案:

答案 0 :(得分:2)

您在该元素上没有ID,更好的方法是在内联处理程序中传递this,并为您的函数添加一个参数:

<input type="password" name="password1"  onchange="passcheck(this)">

function passcheck(elem) {
    var pass = elem.value;
    if (pass.value.length<=6)
    {
        alert("The password must me greater the");
    }

}

答案 1 :(得分:0)

您应该使用OnBlur,因为它会在光标离开文本框时激活