1

I need to use a custom validator for this assignment, and the problem I have to solve is this: I must validate that someone entering a state abbreviation in a form is a valid US state abbreviation (such as AK, AL, AR, etc.). I don't have any syntax errors that I can see, so my form is posting fine, but when I enter an invalid state (such as ZZ), it doesn't give me the error message that should post automatically (in this instance, my error message says "Please enter a valid US state (using all caps)). Here's my control:

     <asp:Label
 id="state"
 text="State:"
 runat="server" />
 <br />
<asp:TextBox
 id="valState"
 MaxLength="2"
 Width="20"
 Runat="server" />
 <asp:CustomValidator
 id="reqState"
 ControlToValidate="valState"
 OnServerValidate="stateArrayCheck"
 Text="Please enter a valid U.S. State (using all caps)"
 Runat="server" />

     <br /><br />

 <asp:Button
 id="btnSubmit"
 Text="Submit"
 Runat="server" />

The logic for my event is here:

    void stateArrayCheck (Object source, ServerValidateEventArgs args)
    {

                ArrayList stateList = new ArrayList();

                stateList.Add("AL");
                stateList.Add("AK");
                stateList.Add("AR");
                stateList.Add("AZ");
                stateList.Add("CA");
                stateList.Add("CO");
                stateList.Add("AL");
                stateList.Add("CT");
                stateList.Add("DE");
                stateList.Add("FL");
                stateList.Add("GA");
                stateList.Add("HI");
                stateList.Add("ID");
                stateList.Add("IL");
                stateList.Add("IN");
                stateList.Add("IA");
                stateList.Add("KS");
                stateList.Add("KY");
                stateList.Add("LA");
                stateList.Add("ME");
                stateList.Add("MD");
                stateList.Add("MA");
                stateList.Add("MI");
                stateList.Add("MN");
                stateList.Add("MO");
                stateList.Add("MS");
                stateList.Add("MT");
                stateList.Add("NC");
                stateList.Add("NE");
                stateList.Add("NH");
                stateList.Add("NJ");                    
                stateList.Add("NM");
                stateList.Add("NY");
                stateList.Add("ND");
                stateList.Add("OH");
                stateList.Add("OK");
                stateList.Add("OR");
                stateList.Add("PA");
                stateList.Add("RI");
                stateList.Add("SC");
                stateList.Add("SD");            
                stateList.Add("TN");
                stateList.Add("TX");
                stateList.Add("UT");
                stateList.Add("VA");
                stateList.Add("VT");
                stateList.Add("NM");
                stateList.Add("WA");
                stateList.Add("WY");                    


                for(int i=0; i <= stateList.Count; i++)
                {
                    if (valState.Text != stateList[i])

                        args.IsValid = false;
                        else
                        args.IsValid = true;

                }
          }








</script>

Not quite sure what the problem is, but my other validators work fine. They're just simple required validators, but the text appears as it's supposed to if you don't enter anything into the text boxes. Any help would be much appreciated. Thank you.

2
  • Why not just use a dropdown list? Commented Nov 26, 2014 at 19:37
  • If this were a real world website, I probably would do that. But this is for schol and we're learning about validation controls, so I have to use one. Commented Nov 26, 2014 at 19:39

2 Answers 2

3

This logic of the for (int i = 0; i <= stateList.Count; i++) loop isn't correct - it keeps toggling the value of args.IsValid without exiting until it completes the loop (and it overruns the upper bound). You can exit as soon as you find a valid state.

Also, if the validation method stateArrayCheck is in the code behind (.aspx.cs, as opposed to a <script runat="server"> tag), you'll need to ensure that the validation method is at least protected scope.

You can also consider moving the list of state constants into a static HashSet - since it is constant, there is no need to instantiate it on each validation call, and 'keyed' sets like HashSets and Dictionaries are well suited for fast, unique lookups.

You can then do the validation in a single line:

// Move the state list to a static / cached initialization
private static HashSet<string> StateList = new HashSet<string>
{
    "AL","AK", ...
};

// The check for valid state is now a simple lookup in the HashSet
protected void stateArrayCheck(Object source, ServerValidateEventArgs args)
{
    args.IsValid = StateList.Contains(valState.Text);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You are close. As StuartLC said, you will set IsValid to false again, even if you found a match before. Since I assume that you are still learning C#, stick with what you got (and understand) for now, and adapt like so:

ArrayList stateList = new ArrayList();

stateList.Add("AL");
/* your entire list here */
stateList.Add("WY");

// expect to not find the state
args.IsValid = false;

// check if you can find the state in your list
foreach (string state in stateList)
{
    if (valState.Text == state){
        args.IsValid = true;
        break;  // no point in checking further elements, break the loop
    }
}

The foreach loop instead of the for loop is just personal preference. However, you can't exeed the bounds of the collection with the foreach loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.