views:

602

answers:

4

in my application i have textbox in that user can't enter digit is there any solution for this thank u

A: 

here is a MSDN and CodeProject article on creating a numeric text box

Andrew Keith
+1  A: 

what programming language? in delphi: add a OnKeyPress event add code

if (( Key >= '0') and(Key <= '9')) then Key := #0;

to this event.

andremo
+1  A: 

there are many ways to tackle your problem

1)you can assign a validator to a textbox and use the numeric only validator.

2)you can fire your own function in textchanged and capture the text either by using a regular expression or by manually checking if the string contains a nubmer or not.

3)You can try this, make a javscript function

 function fncInputNumericValuesOnly(x)
        {

               var txt = document.getElementById(x).value;
    if(txt.length > 1)
    {
    txt = txt.substr(0,1);
    if(!((txt >= 'a' && txt <= 'z') || (txt >= 'A' && txt <= 'Z')))
    event.returnValue = false;
    }
        }

and in the markup of your aspx page have the textbox refer to the function like this

<asp:TextBox id="txtQty"  runat="server" onkeypress= "fncInputNumericValuesOnly(this.id)"/>

Hope this helps.

========UPDATED code to make it accept first letter as character and rest as alphanumeric=======

Since you want to enforce only alphabets in first character so you need to check only for the first character, as you are allowing alphanumerics on all other positions then you don't need to check them. (it will allow symbols too in alphanumerics).

i believe you are doing this for passwords check, it will be much neater if you assign a regular expression validator, which causes a regular expression validation to occur when the password box loses it's focus.

Anirudh Goel
thank u mr>anirudh goel nice name what u do mr.anirudh
Surya sasidhar
but the textbox accepting only digits i want first character must a character and remaining alphanumeric characters
Surya sasidhar
@Surya: Why have you accepted this as your preferred answer if it doesn't meet your requirements?
LukeH
@surya:if you want to accept only characters then you will have to modify the code a little bit, it will become a overkill but here you go. Check the answer again, with edited code.
Anirudh Goel
What happens when JavaScript is turned off? Does this code handle it?
Shaharyar
it will not! That case you must add a server side validation.
Anirudh Goel
A: 

Add a RegularExpressionValidator to the page. Set the ControlToValidate to the ID of the TextBox. And use a ValidationExpression of "[^0-9]." or "[a-zA-Z][a-zA-Z0-9]". The first disallows a digit as the first character, but accepts inputs like "%%". The second one requires an initial letter and then zero or more letters (upper and lowercase) and/or digits.

Hans Kesting