Reputation: 95
The part of this assignment that I'm stuck on is this: The user must enter a state abbreviation in the "State" field (which is a standard textbox). However, I'm supposed to make sure that the the characters entered in the textbox are a valid state abbreviation. So I create an array of all 50 states, and then used a custom validator to accept the user input and compare it to all 50 state abbreviations using a for loop. However, I'm getting an error that says I haven't created a definition for an event (stateArrayCheck) even though I know I have! I have a lot more work to do on this project, so this is really frustrating being stuck on this part of the assignment. Here's my code:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
void stateArrayCheck (Object source, ServerValidateEventArgs args)
{
string[] states = new string[49];
states[0] = "AL";
states[1] = "AK";
...
states[48] = "WI";
states[49] = "WY
for(int i=0, i <= states.count, i++)
{
if(valState.text != states[i])
{
Response.Write("Please enter a valid US state.";
}
}
}
</script>
<style>
#name {
float: left;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id ="name">
<asp:Label id="lblFirstName" Text="First Name:" AssociatedControlID="txtFirstName" Runat="server" />
<asp:TextBox id="txtFirstName" Runat="server" />
<asp:RequiredFieldValidator id="reqFirstName" ControlToValidate="txtFirstName" Text="(Required)" Runat="server" />
</div>
<div>
<asp:Label id="lastName" text="Last Name:" runat="server" />
<asp:TextBox id="txtLastName" Runat="server" />
<asp:RequiredFieldValidator id="reqLastName" ControlToValidate="txtLastName" Text="(Required)"Runat="server" />
<asp:Label id="city" text="City:" runat="server" />
<asp:TextBox id="txtCity" Runat="server" />
<asp:RequiredFieldValidator id="reqCity" ControlToValidate="txtCity" Text="(Required)" Runat="server" />
<asp:Label id="state" text="State:" runat="server" />
<asp:TextBox id="valState" MaxLength="2" Width="20" Runat="server" />
<asp:CustomValidator id="reqState" ControlToValidate="valState" OnServerValidate="stateArrayCheck" Text="(Required)" Runat="server" />
</div>
</form>
</body>
</html>
I know you may not be a huge fan of validators. My teacher isn't either, but they're required for this assignment. Any help would be GREATLY appreciated. Thank you.
Upvotes: 0
Views: 269
Reputation: 335
unless this is a typo the last line is missing a quote and semi colon
states[49] = "WY
You also declare the array for 49 elements then assign 50 elements to it.
string[] states = new string[49];
You should declare for 50 and 49 is the 50th element.
string[] states = new string[50];
Upvotes: 4