简体   繁体   中英

How to make a text box to accept only number based on a condition?

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.

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;
      }
   }
}

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

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;
              }
           }
        }

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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