Results 1 to 5 of 5

Thread: RegularExpressionValidator help with not allowing null or spaces [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    RegularExpressionValidator help with not allowing null or spaces [RESOLVED]

    I am using the RegularExpressionValidator control and I have a form element that I want to allow numbers or a period is all. I believe I have the syntex right with

    VB Code:
    1. <asp:RegularExpressionValidator ControlToValidate="txtRetail" ErrorMessage="Retail" Text="Example:....." runat="server" ValidationExpression="^[0-9]{1,6}\.{0,}[0-9]{0,2}$" />

    Works except if I put no value it gets by. I tried ^\S|.... but that didn't work.

    Any ideas?

    Thanks in advance.
    Last edited by lleemon; May 4th, 2004 at 10:26 AM.

  2. #2
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    You can use a Required Field Validator along side the regularExpressionvalidator.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830
    Fishcake thanks. May not be pretty code but here is what I ended up with:
    VB Code:
    1. .......
    2. <tr>
    3.   <td>Product Wholesale Price:</td>
    4.   <td><asp:textbox ID="txtWholesale" MaxLength="10" runat="server" Text="0" />
    5.   <asp:regularexpressionvalidator ControlToValidate="txtWholesale" ErrorMessage="Wholesale" Text="Example: 12.34; No letters" runat="server" ValidationExpression="^[0-9]{1,6}\.{0,}[0-9]{0,2}$" />
    6.   <asp:requiredfieldvalidator id="RequiredFieldValidator5" runat="server" Text="Requires Number (0)" ErrorMessage="Wholesale" ControlToValidate="txtWholesale"></asp:requiredfieldvalidator></td>
    7. </tr>
    8. .........

  4. #4
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    May not be pretty code but here is what I ended up with:
    But hey if it works.....

    To the best of my knowledge, with a regularexpressionvalidator if the value of the control to validate is empty, no validation functions are called and validation succeeds. So using a req field validator with it is an easy way around it.

  5. #5
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Fishcake has described the proper way to handle it.

    A RegularExpression validator will only run validation if there is a value to validate. If no value exists, how could it tell you if it met the RegEX pattern. Granted, MS could have made another property of the class that also forced a value to be required.

    But in this scenario:
    Say you have a form where only three fields are required: Lastname, FirstName and Date of Birth. But you also have a field for phone number, but that field is optional.

    So the user is not required to enter a phone number, but IF they decide to enter a value in the phone number field, we would use the RegEx validator to validate to see if the value entered is a valid phone number format. If they don't enter a value, we don't care about the field.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width