【问题标题】:Validating a number in a textbox [VB2010]验证文本框中的数字 [VB2010]
【发布时间】:2011-11-19 16:54:19
【问题描述】:

我正在为电影票程序编写应用程序的一部分。

基本上,我必须根据选中的单选框编写一个 If 语句来验证放入文本框中的年龄。

因此,如果选中单选框“PG”,则文本框中的年龄必须等于或大于 12。如果选中“受限”,则文本框必须等于或大于 17。

谁能帮我解决这个问题?我会很感激的。

谢谢!

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    我假设这是一个 asp.net 应用程序?

    如果是这种情况,最好的方法是通过将其 Autopostback 属性设置为 true 来使单选按钮和文本回发。在服务器端,检查组合并采取相应措施。

    使用 Switch 语句根据所选评级检查年龄。

    您可以将它们包装在 UpdatePanel 中以防止可见的回发。

    伪代码:

    rating_changed() {
       checkAge();
    }
    
    txtAge_changed() {
      checkAge();
    }
    
    void checkAge() {
      bool ageOkay = false;
      int age = Convert.ToInt32(txtAge.Text);
    
      switch (rating.SelectedItem.Value) {
         case "G":
            ageOkay = true;
            break;
         case "PG":
            if (age >= 8) ageOkay = true;
            break;
         case "PG-13":
            if (age >= 13) ageOkay = true;
            break;
      } 
    
    if (ageOkay) {
      //do next task
    } else {
      //you're not old enough
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-10
      相关资源
      最近更新 更多