How will you know whether the input entered by a user in a textbox is alphanumeric, number, or string? I request a user to enter a number in a textbox and I wish to trap those users entering alphanumeric or strings. How can I do this?

9 Years
Discussion Span
Last Post by SheSaidImaPregy

you can use javascript to validate input in your textbox :
add this script to head section of your page

<SCRIPT language="JavaScript">

function IsNumeric(strString)
   //  check for valid numeric strings  
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         blnResult = false;
   return blnResult;

  // -->

and add this to your buttons onclientclick attribute as follows:

<asp:button id="foo" runat="server" OnClientClick="return IsNumeric('your textbox's clientID')" /> 

this will prevent page from postback if the value is not numeric.
alternatively you can use the following event of textbox :

<asp:textbox runat="server" id="foo" onkeyup="if(!IsNumeric(this.value){alert('your input is not numeric');this.value='';this.focus();}" /> 

Serkan ┼×endur

Edited by pyTony: fixed formatting


Or you can just automatically make it so your user cannot type in anything but integers, or alphanumeric code, or even whatever else. Something like this would be:

<script language="javascript">

function onlyInts(objEvent) {
  ikeyCode = objEvent.keyCode;
  if (ikeyCode >= 48 && ikeyCode <= 57) { return true; }
  return false;

function onlyAlphaNumeric(objEvent) {
  ikeyCode = objEvent.keyCode;
  if (ikeyCode >= 48 && ikeyCode <= 57 || ikeyCode >= 65 && ikeyCode <= 90 || ikeyCode >= 97 && ikeyCode <= 122) { return true; }
  return false;


Then you can add this programmatically:

controlname.attributes.add("onkeypress","return onlyInts(event)")
controlname.attributes.add("onkeypress","return onlyAlphaNumeric(event)")

Now to stop a user from submitting an empty field, just add a simple control:

<asp:RequiredFieldValidator id="valcontrolname" ControltoValidate="controlname" errorMessage="* " forecolor="#FF0000" display="dynamic" runat="server" />

Just place that infront of your textbox.

Everything that was just done is this:
1. User cannot enter anything except for 0123456789 from both the numbers above keyboard and numpad, or alphanumeric which is all letters small and caps, and integers.
2. a user cannot submit the form unless controlname is not null or empty.
3. If controlname is null or empty, it will post a red star infront of the textbox.


Thanks, it worked perfectly. But what if instead of java script, i'll make use of C# as my script, how can I do this?


there is a very easy way to check it in c# as follows

 int i;
        if (int.TryParse(TextBox1.Text, out i))
            Label1.Text = i.ToString();
            Label1.Text = "your input must be numeric";

Serkan ┼×endur

Edited by pyTony: fixed formatting


I would recommend using both, just as a safe guard for some reason that a user can input something that wasn't typed.

But at a minimum, you should always use server-side validation.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.