【问题标题】:Javascript - Counting number of characters someone has used in their password for validationJavascript - 计算某人在密码中用于验证的字符数
【发布时间】:2012-05-07 06:44:36
【问题描述】:

我正在制作一个表格,老年人必须使用它来注册医疗服务。 (学校项目) 我想这样做,如果输入的字符数少于或多于警报框的允许字符数,以说明他们在最后一个密码中输入了多少字符,以显示他们出错的地方。我已经编写或者更确切地说是复制和编辑了验证代码。

if (pw.length < o.length[8] || pw.length > o.length[25])
alert("Your password must be between 8 to 25 chacters. The one you entered had * characters")
    //Would have to remember how many were entered, and then produce it
    return false;

我做了一些我找不到的研究。 但是,如果您对我的工作方式有一些建议,我将不胜感激。

【问题讨论】:

    标签: javascript validation passwords character counting


    【解决方案1】:
    if (pw.length < 8 || pw.length >25){
        alert("Your password must be between 8 to 25 chacters. The one you entered had "+pw.length+" characters");
        return false;
    }
    

    【讨论】:

    • 缺少大括号会破坏这个sn-p。
    【解决方案2】:

    假设 pw 是您的密码输入框(使用 getElementById 获得),您基本上需要使用 pw.value.length 检查框中值的长度,这将返回一个整数。这应该会让你走上正轨

    【讨论】:

      【解决方案3】:

      在这种情况下o 是什么?

      为什么不

      if (pw.length &lt; 8 || pw.length &gt; 25) ...

      不需要o.length...

      这是您提到的复制和粘贴的问题。你有代码,但不明白。编写自己的代码会更好......

      【讨论】:

        【解决方案4】:

        你正在寻找..

        if(pw.length < 8 || pw.length > 25)
          alert("Bad Password!");
        

        pw.length 返回一个代表密码长度的数字。它不是一个数组,因此您不能像使用o.length[8] 那样尝试访问索引。假设o是另一个字符串。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-16
          • 2011-08-14
          • 2013-12-09
          • 2013-04-14
          相关资源
          最近更新 更多