提问者:小点点

文本框接受字符


我想要一个可以接受的文本框上的javascript验证。

a.字符
b.带数字的字符

它不应该只接受数字

例如,我想要这样的东西:-

Abc123,A7组织。

但我不想喜欢;-

333333 , 2222222.

此外,也不允许使用特殊字符

我尝试了下面的js函数,但它不起作用

function NumersWithCharValidation() {
    var textBoxvalue = document.getElementById('txtNameOfFirm').value;
    alert(textBoxvalue);
    if (textBoxvalue.match("/^(?![0-9]*$)[a-zA-Z0-9]+$/")) {
        alert('Good to go');
    }
    else {
        alert('Only Numbers are not allowed');
    } 
}

<input id="txtNameOfFirm" runat="server" onkeypress="return NumersWithCharValidation();" maxlength="200" type="text" width="65%" />

好心建议什么是错的


共3个答案

匿名用户

将方法更改为

function NumersWithCharValidation(thisObj) {
    var textBoxvalue = thisObj.value;
    if ( textBoxvalue.length > 0 && isNaN( textBoxvalue ) && !textBoxvalue.match( /\W/ ) ) 
    {
        alert('Good to go');
    }
    else 
    {
        alert('Only Numbers are not allowed. Special characters are also not allowed' );
    } 
}

<input id="txtNameOfFirm" runat="server" onkeypress="return NumersWithCharValidation(this);" maxlength="200" type="text" width="65%" />

isNaN("extBoxvalue")将检查值是否为纯数

text Boxvalue. match(/\W/)检查值中是否有任何特殊字符

匿名用户

像这样的正则表达式怎么样?字母的积极前瞻,它适用于您的示例输入。

if(text Boxvalue. match("/^(? =[a-zA-Z])[\w\s,]*$/")) { 警报('Good to go');}else{警报('只有数字不允许'); }

匿名用户

这将不允许用户进一步输入

jQuery("#txtNameOfFirm").keypress(function (event, ui)
{ return NumersWithCharValidation(event) });

function NumersWithCharValidation(evt) {

    if (jQuery("#txtNameOfFirm").val().match(/^[0-9]+$/) == null)
    { return true; } else { return false; }
}