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.