Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 759
  • Last Modified:

Server Validation for six digit numbers:"\d{6}". How to avoid empty input? Thanks.

I have a server validation for ID number validation in the text box field, the regular expression is:
ValidationExpression="\d{6}"
It works well when the user input is not exactly six digit number except it can't detext zero, i.e. when user doesn't input any thing and press enter, it will pass to the next step processing. How to change the regular expression so that it can exclude empty input?

Thanks a lot,
Scott
0
heyday2004
Asked:
heyday2004
6 Solutions
 
ihenryCommented:

First suggestion,
Why not just add a RequireFieldValidator to handle empty input.
0
 
slyckboyCommented:
have you thought about using a  RequiredFieldValidator ?
0
 
ihenryCommented:
One advantage using RequireFieldValidator is that you can detect that the input is empty then you can give better error message.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
vinhthuy_nguyenCommented:
Hi, I give it a test on http://www.3leaf.com/resources/articles/regex.aspx with your express and don't input anything and it didn't pass.
Can you post the code ?
Nice day
0
 
heyday2004Author Commented:
Thanks a lot for the prompt responses. I wanted to know if there is any way to use only one regular expression validation here and also curious why the regular expression "\d{6}" doesn't work for empty input. Below is my code, thanks again for all the answers:

<asp:button id="btnEnter" style="Z-INDEX: 101; LEFT: 656px; POSITION: absolute; TOP: 240px" runat="server" Height="36px" Font-Size="Small" Font-Bold="True" Text="ENTER"></asp:button>

<asp:regularexpressionvalidator id="Regularexpressionvalidator2" style="Z-INDEX: 103; LEFT: 328px; POSITION: absolute; TOP: 288px" Height="24px" Runat="Server" Width="345px" Font-Size="X-Small" Text="Invalid ID number." ControlToValidate="TextBox1"  Display="Dynamic" ValidationExpression="\d{6}">Error! Shoud enter a 6 digit badge number here. <asp:regularexpressionvalidator>

0
 
vinhthuy_nguyenCommented:
Hi Heyday2004,
I think that you can't do the regular expression validate and also check null in <asp:regularexpressionvalidator control.
You should create a requiredfieldvalidator for your textbox, or you can use the custom.
<asp:CustomValidator id="valCustom" runat="server"
    ControlToValidate="textbox1"
    ClientValidationFunction="ClientValidate"
    OnServerValidate="ServerValidate"
    ErrorMessage="*This box is not valid" dispaly="dynamic">*
</asp:CustomValidator>

'and validate what you want in ServerValidate
Sub ServerValidate (objSource As Object, objArgs As ServerValidateEventsArgs)
    ' Code goes here
End Sub
Nice day.
0
 
mmarinovCommented:
Hi heyday2004,

here is the remarks from MSDN :
Note   Validation succeeds if the input control is empty. If a value is required for the associated input control, use a RequiredFieldValidator control in addition to the RegularExpressionValidator control.


Regards!
B..M
mmarinov
0
 
ryerrasCommented:
Hi heyday,
 What mmarinov said is 100% correct. You can not validate against a empty control using RegularExpressionValidator or CustomValidator or RangeValidator or any other type validator except RequiredFieldValidator. Usually when you need to use any of these mentioned validators, they should be coupled with RequiredFieldValidator, which kicks in prior to your RegularExpressionValidator, and throws an error. This is the common mistake we all do.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now