Hi,
i am having problems trying to validate my form controlls which are all text boxes. I am using a switch statment to testing things like if the fields are blank ect. However, when i run my app and place an @ sign in my postcode field for example, with all the other text boxes left blank. It shows my messegboxes correctly up to the point at with it sees the @ sign. Instead it displays my user generated message of: " Job center fied cannot be blank". However, i have a check for the @ sign . Why does it not display the correct message when it sees an @ sign?
private void cmdAdd_Click(object sender, EventArgs e)
{
JobCenter jCenter = new JobCenter();
string jobCenterName;
string jobCenterAdd;
jCenter.set_jobCenterName(txtJobCenterName.Text) ;
jobCenterName = jCenter.get_jobCenterName();
switch (jobCenterName)
{
case "":
MessageBox.Show("Job Center Name cannot be blank");
this.allClear();
break;
case "@":
MessageBox.Show("Job Center Name field cannot contain special characters e.g @ ");
this.allClear();
break;
case "+": // does not seemt to validate
MessageBox.Show("Job Center Name will not take values such as positive + sign");
this.allClear();
break;
case "-":
MessageBox.Show("Job Center Name will not take minus values");
this.allClear();
break;
case "!":
MessageBox.Show("Job Center Name field will not accept specail characters such as exclamtion mark");
this.allClear();
break;
default:
return;
}
string jobPostCd;
jCenter.set_jobCenterPostCode(txtJobCenterPostCode.Text);
jobPostCd = jCenter.get_jobCenterPostCode();
switch (jobPostCd)
{
case "":
MessageBox.Show("Job Center Post Code cannot be blank");
this.allClear();
break;
case "@":
MessageBox.Show("Job Center Post Code field cannot contain special characters e.g @ ");
this.allClear();
break;
case "+":
MessageBox.Show("Job Center Post Code will not take values such as positive + sign");
this.allClear();
break;
case "-":
MessageBox.Show("Job Center Post Code will not take minus values");
this.allClear();
break;
case "!":
MessageBox.Show("Job Center Post Code field will not accept specail characters such as exclamtion mark");
this.allClear();
break;
default:
return;
}
I have skipped the other case stements because they are doing the same thing.
Loading
Amit ChoudharyPosted Jan 18, 2011, 7:17 AM
In below lines of code i have analysed,
jCenter.set_jobCenterName(txtJobCenterName.Text) ;
jobCenterName = jCenter.get_jobCenterName();
switch (jobCenterName)
You are not passing the text of text box. but you are actually passing the text to some function and then retrieving the value
form another function. may be it is not returning the value may be you might have applied some filter over there.
if you want to check your inputs from text box then directly pass the text box value to the switch statement
e.g., switch(txtJobCenterName.Text)
Thanks