2016-05-10 22 views
2

我想创建一个用户名输入,我想限制用户的输入onkeypress。可以输入的字符数限制是20 + 1是固定的前缀。如何限制连续使用相同的字符?

我想实现的是:

  1. 要限制用户输入并只接受A-Z,0-9,破折号( - )和下划线(_)。 [解决]
  2. 禁止用户使用破折号( - )或下划线(_)启动他们的用户名。 [解决]
  3. 禁止用户使用破折号( - ),如果他们使用了下划线(_)和相反。 [已解决]
  4. 允许用户最多使用3个破折号( - )或3个下划线(_)。 [解决破折号]
  5. 要禁止用户使用连续的符号,像这样(user___或用户 - P等)[!求助]

似乎我无法弄清楚是怎么将下划线(_)限制为3 max,并且如何停止连续破折号( - )和下划线(_)。

任何帮助和正确的解释将不胜感激!

我的HTML:

<form name = "RegForm" method="post" action="index.html" class="login"> 
    <input type="text" maxlength = "21" name="userID" id="userID"/> 
</form> 

我的javascript:

var userID_textfield = document.forms.RegForm.userID; 
userID_textfield.onkeypress = function(e) { 

    // Invalid character list 
    var prohibited = "[email protected]#$%^&*()+=;:`~\|'?/.><, \""; 
    // List of characters that can't be first 
    var posRestricted = "-_"; 
    // List of characters that can't be used more than once 
    var numRestricted = "-_"; 
    // Get the actual character string value 
    var key = String.fromCharCode(e.which); 

    /* Goal: 
     Validate: Accept only a-z, 0-9, - and _ */ 
    if (prohibited.indexOf(key) >= 0) { 
     console.log('Invalid key pressed');  
     return false; 
    } 
    else { 
     /* Goals: 
      1. Validate: -, _ and 0-9 can't be first 
      2. Validate: - and _ can't be last if the userID is 21 characters long */ 
     if ((posRestricted.indexOf(key) >= 0 && this.value === "@") || (numRestricted.indexOf(key) >= 0 && this.value.length === 20)) { 
      console.log("Username can't start with a number, a dash (-) or an underscore (_)"); 
      return false; 
     } 
     /* Goals: 
      1. Validate: - and _ can't be used more than once each 
      2. Validate: if - exists _ can't be used and the opposite' */ 
     else if (numRestricted.indexOf(key) >= 0) { 
      var numResValue = [0, 0]; 
      for (var a = 0; a < numRestricted.length; a++) { 
       for (var b = 0; b < this.value.length; b++) { 
        if (this.value.charAt(b) === numRestricted[a]) { 
         numResValue[a] += 1; 
        } 
       } 
      } 
      for (var c = 0; c <= numResValue.length; c++) { 
       if (numResValue[c] < 3) { 
        if (this.value.indexOf(numRestricted.replace(key, "")) === -1) { 
         return true; 
        } 
        else { 
         return false; 
        } 
       } 
       else { 
        return false; 
       } 
      } 
     } 
     else { 
      return true; 
     } 
    } 
}; 

您可以在行动here查看代码。

+0

为5 。仅适用于 - 和_?还是所有角色? – Arg0n

+0

它适用于“ - ”和“_”。其余的字符,除了A-Z,0-9, - 和_都被禁用。 –

+0

你可以用正则表达式来完成,或者你可以通过将最后一个字符的跟踪保存在像s.charAt(s.length-1)这样的字符串中,并在你选择的eventListener中禁止新字符。 – Redu

回答

1

,你可以补充一点:(我更新了小提琴:https://jsfiddle.net/ck7f9t6x/5/

var lastKey = this.value.charAt(this.value.length-1); 
      alert("lastKey = " + lastKey); 
      if(lastKey == key){ 

      return false; 
      } 
      else{ 
      var numResValue = [0, 0]; 
      for (var a = 0; a < numRestricted.length; a++) { 
      for (var b = 0; b < this.value.length; b++) { 
       if (this.value.charAt(b) === numRestricted[a]) { 
       numResValue[a] += 1; 
       } 
      } 
    ... 

} 
+0

非常好@Asma。没有更多连续的破折号和下划线!目前只有下划线限制。虽然我已将它们与短划线编码在一起不超过3,但下划线不符合短划线的要求!你知道这可能是什么原因吗? –

+1

其实这里var numResValue = [0,0];在这里你有 - 然后_的计数器,所以你不得不只测试其中一个计数器的值(Dash或下划线),就像在这里更新的JSFiddle一样:https://jsfiddle.net/ck7f9t6x/ 6/ – Asma

+0

它的工作原理!但是,你能解释下面这一行是什么吗? 我以前从未见过这样的事情:p var indToTest = key === numRestricted [indDash]? indDash:indUnder; –

0

你可以写这个东西,将接受您的字符串,并返回true或false功能(有效与否)

function checkConsecutive(string){ 
//break it to array 
var a = string.split(''); 
//check for consecutive 
for(i=0;i<a.length;i++){ 
if(a[i] == a[i+1]){ 
return true; 
}else{ 
return false} 
} 
} 

我知道这是不是一个完美的解决方案,但它可以工作。

+0

,你可以改变一些东西来调用这个函数。 – 92sharmasaurabh

+0

嘿@ 92sharmasaurabh,我非常难以理解你的代码。我看到它的方式是检查我键入的下一个字符,所以它实际上检查尚未键入的字符。我不认为这可以工作。如我错了请纠正我。 –

+0

对于麻烦的家伙感到难过... plz检查http://plnkr.co/edit/veuJdKanCcZSRf2BUmHt?p =预览这对我来说很好。 – 92sharmasaurabh

0

this的jsfiddle

HTML

<input type="text" onkeypress="preventConsecutive(this, event)"/> 

的JavaScript

function preventConsecutive(el, evt) { 
    if(el.value.length > 0) { 
    var lastChar = el.value[el.value.length-1]; 
    if((lastChar === "-" && evt.keyCode === 45) || (lastChar === "_" && evt.keyCode === 95)) { 
     evt.preventDefault(); 
     return; 
    } 
    } 
} 
+0

那么,你可以在你的代码中替换变量名称。'el' ='userID_textfield','evt' ='e' ..并将它放在你自己的'onkeypress'函数中。 – Arg0n

+0

我做到了!这就是我知道它的工作原理:)有一件事我不明白,但是这条线是如何工作的:_var lastChar = el.value [el.value.length-1]; _ 我不知道你可以得到这样的性格。我虽然只能使用.charAt()来完成。谢谢你让我知道。 –

相关问题