Number validation in required field validator

后端 未结 7 1420
挽巷
挽巷 2021-02-05 08:08

Is it possible to put Number validation in required field validator in asp.net text box?

相关标签:
7条回答
  • 2021-02-05 08:21

    Another possibility is using the RegexpValidator and adding a regular expression that makes sure there's 1 or more digits in it, something like:

    RegularExpressionValidator regexpvalidator = new RegularExpressionValidator(); 
    regexpvalidator.ID = "RegularExpressionValidator1"; 
    regexpvalidator.ValidationExpression = "\d+"; 
    regexpvalidator.ControlToValidate = "YourControl"; 
    regexpvalidator.ErrorMessage = "Please specify a digit"; 
    regexpvalidator.SetFocusOnError = true; 
    
    0 讨论(0)
  • 2021-02-05 08:25

    Actually you only need a regularexpression validator for this purpose with ValidationExpression = "^\d+?$"

    0 讨论(0)
  • 2021-02-05 08:25

    No, a RequiredFieldValidator can only verify that the field contains something.

    If you want to verify that the field only contains digits, you can use a RegularExpressionValidator with the pattern "\d+".

    0 讨论(0)
  • 2021-02-05 08:26

    A RequiredFieldValidator only checks if the field is filled in. It doesn't care what with.

    You will need an extra CompareValidator with it's Operator set to DataTypeCheck and it's Type set to Integer. Note you need both: the CompareValidator will ignore an empty input.

    0 讨论(0)
  • 2021-02-05 08:27

    Maybe you can use a RangeValidator attached to that textbox, setting Type to Integer or wathever.

    RangeValidator class on MSDN

    0 讨论(0)
  • 2021-02-05 08:39

    Yes, like this:

    <asp:TextBox ID="tb" runat="server"></asp:TextBox>
    <asp:RangeValidator ControlToValidate="tb" Type="Integer"></asp:RangeValidator>
    
    0 讨论(0)
提交回复
热议问题