如何限制同一个字符连续使用
How to restrict the same character from being used consecutively?
我正在尝试创建一个用户名输入,我想限制用户在按键上的输入。可以输入的字符数限制为固定前缀20+1。
我想要实现的是:
- 限制用户输入并仅接受A-Z、0-9、短划线(-)和下划线(_)。[已解决]
- 禁止用户以短划线(-)或下划线(_)作为用户名的开头。[已解决]
- 如果用户使用了下划线(_)和相反的下划线,则禁止用户使用短划线(-)。[已解决]
- 允许用户最多使用3个短划线(-)或3个下划线(_)。[虚线已解决]
- 禁止用户使用这样的连续符号(user___或user-p等)[需要帮助!]
我似乎不知道如何将下划线(_)限制为3个最大值,以及如何停止连续的短划线(-)和下划线(_。
如有任何帮助和适当的解释,我们将不胜感激!
我的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 = "!@#$%^&*()+=;:`~'|'?/.><, '"";
// List of characters that can't be first
var posRestricted = "-_0123456789";
// 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;
}
}
};
您可以在此处查看正在执行的代码。
您可以添加以下内容:(我更新了fiddle: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;
}
}
...
}
您可以为这个东西编写一个函数,它将接受您的字符串并返回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}
}
}
我知道这不是一个完美的解决方案,但它是可行的。
请参阅此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;
}
}
}
相关文章:
- 如何限制同一个字符连续使用
- 如何检测每个连续字符与前一个字符相差最小字母差的字符序列(1)
- 如何在不丢失格式的情况下连续淡入() 元素文本的每个字符,包括嵌套元素
- Javascript 密码不应包含用户的帐户名或用户全名中超过两个连续字符的部分
- Javascript 正则表达式避免连续两次使用相同的字符
- 正则表达式不允许连续使用多个特殊字符
- 识别 Javascript 字符串中的连续十六进制字符块
- 正则表达式拒绝连续字符
- 匹配集合中连续出现的多个字符
- 将文本输入限制为由非连续字符分隔的数字组
- Regex电子邮件验证-域中没有连续的连字符
- 当4个字母字符连续出现时,Regex进行过滤
- 需要使用JavaScript过滤掉字符串中重复的连续字符
- 在JavaScript中验证连续字符或数字的正则表达式
- 如何找到3个或更多的连续字符
- Regex:两个或多个连续字符或字符序列
- 用javascript将连续字符序列隐藏到数组中
- Regex拒绝包括大写字母在内的连续字符
- 字符串必须匹配 set 中的 7 位数字和可选的非连续字符
- JavaScript 中的正则表达式拒绝 3 个连续字符