1

I'm currently wondering about server side validation of a field.

For example I have a field which can't be blank, must be a number & must be under the value of 999999.

Example code to validate would be:

 If checkNumeric(txtBaseSalary.Text) = True Then
            txtBaseSalary.CssClass = "text"
        Else
            x += 1
            step_4 += 1
            txtBaseSalary.CssClass = "text error"
 End If

If val(txtBaseSalary.Text) = 0 or val(txtBaseSalary.Text) > 999999  Then
            x += 1
            step_4 += 1
            txtBaseSalary.CssClass = "error text"
        Else
            txtBaseSalary.CssClass = "text"
End If

However I'm sure you've spotted the issue, if it fails the first validation but passes the second the error amount (step_4) will go up, however if passes the second then the visual representation is cleared.

Does anyone have any implementations of doing multiple validations against a field without nested if statements?

As well, please provide solutions that could apply to any situation where multiple validation is required, not simply here is a way you can validate that one situation I've posted.

Anthony Grist
  • 38,173
  • 8
  • 62
  • 76
Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76
  • Why not use ASP.NET validation controls http://msdn.microsoft.com/en-us/library/aa479013.aspx – Priyank Patel Oct 29 '12 at 12:12
  • 1
    Use a `CustomValidator` with `ValidateEmptyText=true`. Use a `regex` then. – Tim Schmelter Oct 29 '12 at 12:13
  • If it fails the first do you even need to check the second condition? If not, use an "else if" so it stops after the first fail. (note that there are several good references others are making to use out-of-the-box validation.) – Ray K Oct 29 '12 at 12:22
  • Correct I don't need to do a second check if the first fails @RayK however I want to avoid having multi if statements due to the fact I could have say 5 checks against a field, dependent upon other inputs. – Ryan McDonough Oct 29 '12 at 12:40
  • @TimSchmelter can the CustomValidator be extended to allow me to specify new checks and create regex for the new checks? – Ryan McDonough Oct 29 '12 at 12:40
  • @RyanMcDonough: A `CustomValidator` can be extended to allow anytghing. It's the most flexible but also the most sophisticated validator type. I would go with the `RequiredFieldValidator` + `CustomValidator` approach. – Tim Schmelter Oct 29 '12 at 12:58

3 Answers3

3

You can either

  • use a CustomValidator with ValidateEmptyText=true and code that checks your condition or
  • use a RequiredFieldValidator that checks for empty text and a CompareValidator that checks for a numerical value in a given range.

<asp:CompareValidator runat="server" 
    id="cmpNumbers" 
    controltovalidate="txtBaseSalary" 
    valuetocompare="999999"
    operator="LessThan" 
    type="Integer" 
    errormessage="The number should be smaller than 999999!" /><br />

You could use an Ajax ValidatorCalloutExtender to apply your CSS on error. Another option is to use JQuery.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I do like the sound of that, can I extend that and add my own checks, so for example could I do 4 different validations against that field? – Ryan McDonough Oct 29 '12 at 12:41
  • Or would I have say 4 different of these asp:comparevaldiator 's under the control? – Ryan McDonough Oct 29 '12 at 12:44
  • @RyanMcDonough: A `CustomValidator` can be extended to allow anytghing. It's the most flexible but also the most sophisticated validator type. I would go with the `RequiredFieldValidator` + `CustomValidator` approach. You can provide one error message per validator. So it's better to provide a different validator for every check, then you can also provide different error-messages. It's also easier to enable/disable validators or asign differerent [`Validation Groups`](http://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx) for each validator. – Tim Schmelter Oct 29 '12 at 13:00
1

Better you can create a Numeric text box control. It will avoid checking of checkNumeric.

andy
  • 5,979
  • 2
  • 27
  • 49
1

A single regex compare on this expression

^([0-9]{1,6})$

will force it to be numeric and between 0 and 999999.

You can either use the method you wrote (since you seem to want to increment 'x' and 'step_4')

using System.Text.RegularExpressions;
Regex r = new Regex("^([0-9]{1,6})$");
Match m = r.Match(text);
if (m.Success)...  

MSDN Documentation

Or you can use an out-of-the-box validator:

 <asp:RegularExpressionValidator 
   ID="regexpName" runat="server"     
   ErrorMessage="error text." 
   ControlToValidate="txtName"     
   ValidationExpression="^([0-9]{1,6})$" />

More MSDN Documentation

Ray K
  • 1,452
  • 10
  • 17
  • Cheers for the reply, though as I said I was hoping for answers that would be general enough to apply for any situation I want to do multiple validation rather than just this one instance. Though it does seem regex is the answer and do multiple checks in one go. – Ryan McDonough Oct 29 '12 at 12:59