I need a textbox that does not let me write numbers

0

I have a textbox in the webform on asp.net (without MVC), I need you to just write lyrics

    
asked by Manuel N 15.11.2018 в 21:06
source

1 answer

2

The first thing you should do is create your JavaScript where you indicate by a regular expression that you only have to admit letters, otherwise you cancel the evento that you trigger when you press a key:

function SoloLetras(e) {
    if (!(/[A-Za-z]/.test(e.key)))
        e.preventDefault();
}

Add the TextBox to the web form:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

And in the CodeBehind you add the attribute to that control in this way, what is done here is that you send the event every time you press a key to the function of JavaScript :

TextBox1.Attributes.Add("onkeypress", "SoloLetras(event);");

You also have this way, what you do is that you return a bool in the function every time you meet the condition of Regex if it is a letter will return true and allow the entry of the text:

function SoloLetras(e) {
    return (/[A-Za-z]/.test(e.key));
}

And so in the CodeBehind :

TextBox1.Attributes.Add("onkeypress", "return SoloLetras(event);");

Documentation about the use of .test and an expression regular.

    
answered by 15.11.2018 / 22:08
source