0

How to make a textbox to accept only number? when my label.text is gms or rs or knot, then respective textbox accept only numbers. when label.text is character it has allow only character value.

Example:gms: 1200
        character:black
        knot:5
        rs:80
        character:pink

This order may change based on the selection. And please post the ASPX code too.

Gopi
  • 140
  • 2
  • 8
  • 17

5 Answers5

1

Can you add the KeyPress event for the corresponding textbox? So that you can do the following!

private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
   if ((label.Text.Equals("gms") || label.Text.Equals("rs") || label.Text.Equals("knot"))
   { 
      if (!char.IsDigit(e.KeyChar))
      {
         e.Handled = true;
      }
   }
   else
   {
      if (!char.IsLetter(e.KeyChar))
      {
         e.Handled = true;
      }
   }
}
Vanest
  • 906
  • 5
  • 14
  • hi @vanest i have one doubt.In .aspx page should i have to add textbox_keypress property .If i have to add that too please post that coding too – Gopi Sep 21 '13 at 08:45
  • This sample is for windows application! :-( – Vanest Sep 23 '13 at 05:43
0

Apply a regular validator on your text box with validation expression "^[0-9]" which accept only numbers initially make it disabled

you can then enable disable it according to the text of your label making the text box to accept all or only numbers

0

Try this, maybe this is something that you're looking for:

private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
           if ((label.Text.Equals("gms") || label.Text.Equals("rs") || label.Text.Equals("knot")))
           { 
              if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
                {
                    e.Handled = true;
                }

              // only allow one decimal point
              if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
              {
                  e.Handled = true;
              }
           }
        }
Sylca
  • 2,523
  • 4
  • 31
  • 51
  • how to call this text_keypress function from ASPX page – Gopi Sep 26 '13 at 16:30
  • Here is an example: http://stackoverflow.com/questions/13158752/fire-event-on-enter-key-press-for-a-asp-net-textbox – Sylca Sep 27 '13 at 09:20
  • @Sylca : I think KeyPressEventArgs cannot be called in web applications (System.Web doesn't have definition for that, its a member of System.Windows.Forms)... – Vanest Oct 07 '13 at 07:54
0

Although you want to implement the solution in . Better suggestion would be is to implement in .

Add simple javascript function to in aspx page and no code in codebehind file.

$(document).ready(function () {

var arrForNum = ['gms', 'rs', 'knot']; //Your list of label texts for Number only textboxes
// Now traverse for all textboxes where u want to add some restrictons

$('body').find('.customonly').each(function () {
    var id = this.id;
    var res = $('label[for=' + id + ']').text();    
   // check if its the array we declared else it will be charecters only. 
    if ($.inArray(res, arrForNum) >= 0) {
        $(this).forceNumeric();  // Added simple function in fiddle.
       //You can apply any other function here if required.
    } else {
        $(this).forceCharecters('chars');
    }
  });
});

Check the JsFiddle for detailed code.

Pratik
  • 1,472
  • 7
  • 20
  • 36
0
private void txt3_KeyPress(object sender, KeyPressEventArgs e)
{

    for (int h = 58; h <= 127; h++)
    {
        if (e.KeyChar == h)             //58 to 127 is alphabets tat will be         blocked
        {
            e.Handled = true;
        }

    }

    for(int k=32;k<=47;k++)
    {
        if (e.KeyChar == k)              //32 to 47 are special characters tat will 
        {                                  be blocked
            e.Handled = true;
        }

    }



}

try this is very simple

rithish
  • 131
  • 1
  • 6