|
-
May 3rd, 2004, 04:47 PM
#1
Thread Starter
Fanatic Member
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:
<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.
-
May 3rd, 2004, 07:59 PM
#2
You can use a Required Field Validator along side the regularExpressionvalidator.
-
May 4th, 2004, 10:25 AM
#3
Thread Starter
Fanatic Member
Fishcake thanks. May not be pretty code but here is what I ended up with:
VB Code:
.......
<tr>
<td>Product Wholesale Price:</td>
<td><asp:textbox ID="txtWholesale" MaxLength="10" runat="server" Text="0" />
<asp:regularexpressionvalidator ControlToValidate="txtWholesale" ErrorMessage="Wholesale" Text="Example: 12.34; No letters" runat="server" ValidationExpression="^[0-9]{1,6}\.{0,}[0-9]{0,2}$" />
<asp:requiredfieldvalidator id="RequiredFieldValidator5" runat="server" Text="Requires Number (0)" ErrorMessage="Wholesale" ControlToValidate="txtWholesale"></asp:requiredfieldvalidator></td>
</tr>
.........
-
May 4th, 2004, 11:27 AM
#4
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.
-
May 13th, 2004, 07:02 AM
#5
I wonder how many charact
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|