简体   繁体   English

文本框接受字符和数字或字母,但不接受数字

[英]Textbox to accept charcters & numbers or Alphabets but not ONLY numbers

I want a javascript validation on a textbox which can accept. 我想在可以接受的文本框中进行JavaScript验证。

a. 一种。 Characters 人物
b. Characters with Numbers 带数字的字符

It should not accept only numbers 它不应该只接受数字

For example i want something like:- 例如我想要类似的东西:

Abc123 , A7 organisation . Abc123A7组织

but I dont want like;- 但我不想;-

333333 , 2222222 . 333333,2222222。

Also, special characters are also not allowed 另外,也不允许使用特殊字符

I tried like below js function but it is not working 我试过像下面的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%" />

kindly suggest what is wrong 请暗示出什么问题了

change the method to 将方法更改为

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( "textBoxvalue" ) will check if the value is a pure number isNaN( "textBoxvalue" )将检查该值是否为纯数字

textBoxvalue.match( /\\W/ ) checks if there is any special character in the value textBoxvalue.match( /\\W/ )检查值中是否有任何特殊字符

How about a regex like this? 这样的正则表达式怎么样? Positive lookahead for letters, it works for your example inputs. 正字母的前瞻性,适用于示例输入。

if (textBoxvalue.match("/^(?=[a-zA-Z]+)[\\w\\s,]*$/")) { alert('Good to go'); } else { alert('Only Numbers are not allowed'); }

This will not allow the user to type any further 这将不允许用户进一步输入

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

and

function NumersWithCharValidation(evt) {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM