2013-06-18 74 views
1

我在限制中遇到困难文本框值。TEXTBOX限制符号,字符和数字

该文本框不应该允许除"?*,"以外的符号。

例:A123?*,B456?*,C789?*,D126?*,E?*666

之后和逗号之前(,)10 Characters/Symbols/Numbers是允许的。

只有54 characters允许在文本框内包括逗号。

只有4 comma's允许在TEXTBOX内。

可能出现的文本框中输入:ABC?12345*,A?BC*12345,A???1234**,?*C?12345*,ABC?123***

另外,符号和字符是可更换的,无论是在开始 - 符号可能出现或字符或数字。

请检查图像以获得更多解释。 enter image description here

我试图限制与其他符号和允许的逗号,但我面临困难与10个字符限制,并允许符号和字符数。

//<![CDATA[ 
window.onload = function() { 
    /*var f = document.getElementById('textbox_restrict'); 
    f.addEventListener('keyup', function(evt){ 
    alert("Vinayagam"); 
    var regex = /[^a-zA-Z0-9]/; 
    if(regex.test(this.value)) { 
    this.value = this.value.replace(regex, '') 
    } 
    });*/ 
    $('#textbox_restrict').keypress(function(e) { 
     $('#backgroundPopup').animate({'opacity': 0.0, 'display': 'none'}); 
     this.value = this.value.replace(/[^0-9a-zA-Z\*\?\{0,12}]/g, '').toUpperCase(); 
    }); 

}//]]> 

$(function() { 
    $('body').on('keyup', 'input[id^="textbox_restrict"]', function() { 
     this.value = this.value.replace(/[^0-9a-zA-Z\*\?]/g, '').toUpperCase(); 
    }); 

    $('#search').live('click', function() { 
    }); // End of save click function 
}); // End of function 

I also took reference to this Question but the senario is different: How restrict textbox in C# to only receive numbers and (dot "." or comma ","), after "." or "," only allow 2 number characters

Regex Replace anything but numbers and lowercase

jQuery keyup() illegal characters

请咨询我!我有点困惑实施它。

+1

你需要像这样的东西[jsFiddle](http://jsfiddle.net/U5zq3/1/)? – Teemu

+0

@Teemu理解工作代码将会非常有帮助。提前致谢。 – TomPHP

+1

呃......我不太理解你的评论,你是否希望我将该片段作为答案发布?尽管你已经接受了答案。 – Teemu

回答

1

也许最好不要试图一次完成所有的东西。为了简化每个阶段,我把这个任务分成了几部分。只需将以下代码片段添加为keyup或更确切的说是oninput事件处理程序到您的input

keyUp = function() { 
    var n, parts, temp, 
     text = this.value, 
     caretPos = this.selectionEnd; // Stores the caret position 
    parts = text.split(','); // Creates an array of comma separated values 
    while (parts.length > 5) { // Limits the array length to 5 i.e only 4 commas allowed 
     parts.pop(); 
    } 
    for (n = 0; n < parts.length; n++) { // Iterates through each comma separated string 
     parts[n] = parts[n].substring(0, 10); // Limits the string length to 10 
     temp = parts[n].match(/[0-9a-zA-Z\*\?]/g); // Creates an array of acceptable characters in the string 
     if (temp) { 
      parts[n] = temp.join(''); // Adds the accepted value to the original 
     } else { // If not value, add empty to the original 
      parts[n] = ''; 
     } 
    } 
    this.value = parts.join(',').toUpperCase(); // Creates a new acceptable value to the input 
    this.setSelectionRange(caretPos, caretPos); // Returns the caret to the original position 
    return; 
} 

现场演示jsFiddle

EDIT

删除字符串剿至54中的字符串的中间添加字符时要避免在字符串的末尾意外删除的字符。增加else这将在未找到可接受的值的情况下添加空字符串。看起来像oninput将是这个代码更好的事件。

编辑II

增加了插入位置返回到原来位置后编辑在它的中间的字符串。当用鼠标粘贴文本时不完美,但似乎在键盘输入时起作用。 (小提琴链接已更新。)

+0

它很棒!像魅力一样工作。由于功能和逻辑我接受了你的答案。因为我们很高兴能够通过工作示例获得答案。一旦我的投票结果有效,我会加倍努力。感谢你的代码。 – TomPHP

+0

我也喜欢接受保罗的答案,但叠加流程受到限制? (它对我来说很复杂,因为我会使用动态文本框。) – TomPHP

+0

我在代码中遇到了一个小问题..我的左箭头不工作..我无法移动到左边。如果我使用KEYPRESS但是TEXTBOX中的最后一个字符不会被转换为大写:) :( – TomPHP

1

这个怎么样正则表达式

/^(?:[a-z\d?*]{1,10}(?:,|$)){1,5}$/i 

从字符串的开始,允许字符a-z,数字,?*,有1和这些10,然后是一个逗号或间字符串的结尾。至少重复一次,最多重复5次。该字符串必须结束。 i标志使a-z包括A-Z

'A123?*,B456?*,C789?*,D126?*,E?*666'.match(/^(?:[a-z\d?*]{1,10}(?:,|$)){1,5}$/i); 
// ["A123?*,B456?*,C789?*,D126?*,E?*666"] 

应当指出的是,这不会阻止*??x**x?,也不会阻止一个逗号在字符串的结尾。

+0

如您所述“应该指出,这不会阻止* *,?x *或* x ?.”所以我想添加另一个函数来检查符号? – TomPHP

+0

另外符号和字符是可替换的,无论是在开始 - 符号可能会来或字符或数字。 – TomPHP

+1

因为我目前它不关心字符顺序,所以我的笔记。由于'* *'是两个字符,所以你的'10个字符/符号/数字是允许的'的困难。 –