Reputation: 325
I am attempting to make a form where if information is not input, you get a series of error messages depending on what's not there. Unfortunately, I've run into an issue where the variable Msg that holds the messages is not working (Will make sense in a moment I think). It does display the errors, but in a paragraph form, not in a list.
How can I compile all errors into a message with line breaks?
I've tried to include "\n" and "\r\n" to no avail.
What I have now is this:
Msg = Msg + "Text goes here for error messages...";
Code:
private void btnSubmit_Click(object sender, EventArgs e)
{
//DECLARATIONS
int count = 0;
string Msg = "";
Boolean validatedState = true;
Boolean validateEntry = false;
DateTime endDate = new DateTime(2016, 03, 01);
DateTime startDate = new DateTime(2016, 03, 01);
//BEGIN SERIES OF IF/ELSE FOR CONFIRMING ENTRIES
if (Request["txtFirstName"].ToString().Trim() == "")
{
//displays yellow bg for missing input
txtFirstName.BackColor = System.Drawing.Color.Yellow;
Msg = Msg + "Please Enter a First Name" + "\r\n";
}//endif
else
{
txtFirstName.BackColor = System.Drawing.Color.White;
count += 1;
}//end else
if (Request["txtLastName"].ToString().Trim() == "")
{
//displays yellow bg for missing input
txtLastName.BackColor = System.Drawing.Color.Yellow;
Msg = Msg + "Please Enter a Last Name";
}//endif
else
{
txtFirstName.BackColor = System.Drawing.Color.White;
count += 1;
}//end else
if (Request["txtPayRate"].ToString().Trim() == "")
{
//displays yellow bg for missing input
txtPayRate.BackColor = System.Drawing.Color.Yellow;
Msg = Msg + "Please Enter a Pay Rate";
}//endif
else
{
txtFirstName.BackColor = System.Drawing.Color.White;
count += 1;
}//end else
if (Request["txtStartDate"].ToString().Trim() == "")
{
//displays yellow bg for missing input
txtStartDate.BackColor = System.Drawing.Color.Yellow;
validateEntry = false;
Msg = Msg + "Please Enter a Start Date";
}//endif
else
{
startDate = DateTime.Parse(Request["txtStartDate"]);
validateEntry = true;
}//end else
if (Request["txtEndDate"].ToString().Trim() == "")
{
//displays yellow bg for missing input
txtEndDate.BackColor = System.Drawing.Color.Yellow;
validateEntry = false;
Msg = Msg + "Please Enter an End Date";
}//endif
else
{
endDate = DateTime.Parse(Request["txtEndDate"]);
validateEntry = true;
}//end else
//END SERIES OF IF/ELSE FOR CONFIRMING ENTRIES
//START IF VALIDATE ENTRY
if (validateEntry == true)
{
if (DateTime.Compare(startDate, endDate) >= 0)
{
txtStartDate.BackColor = System.Drawing.Color.Yellow;
txtEndDate.BackColor = System.Drawing.Color.Yellow;
Msg = Msg + "The end date must be a later date than the start date.";
//The Msg text will be displayed in lblError.Text after all the error messages are concatenated
validatedState = false;
//Boolean value - test each textbox to see if the data entered is valid, if not set validState=false.
//If after testing each validation rule, the validatedState value is true, then submit to frmPersonnelVerified.aspx, if not, then display error message
Response.Write("<span style= 'BackColor:red'>Msg/<span>");
}
else //goes to this is dates are correct
{
validatedState = true;
count += 2;
txtStartDate.BackColor = System.Drawing.Color.White;
txtEndDate.BackColor = System.Drawing.Color.White;
}
}
//END IF VALIDATE ENTRY
//CONFIRMS ALL ARE CORRECT
if (count == 5 && validatedState == true)
{
Session["txtFirstName"] = txtFirstName.Text;
Session["txtLastName"] = txtLastName.Text;
Session["txtPayRate"] = txtPayRate.Text;
Session["txtStartDate"] = txtStartDate.Text;
Session["txtEndDate"] = txtEndDate.Text;
Response.Redirect("frmPersonnelVerified.aspx");
//sends to other page
}
else
{
Response.Write(Msg);
}
//ENDS CONFIRMATION OF CORRECT DATA
}//end Function: private void BtnSubmit_click...
}[![Current Error Response][1]][1]
Upvotes: 1
Views: 127
Reputation: 3355
If you think the built in validation methods are not sufficient or efficient enough or somehow insecure in some way or for any other reason desire to then I encourage you to encapsulate your validation within a delegate which can be invoked when required for example:
bool Invalidate(Session session)
Determine the logic required in the function such as if the session has the member you require to validate or otherwise.
If the result is true the Session is not valid otherwise it is.
Derive this work flow to encapsulate your requirements further.
bool InvalidateWorkFlowXYZ(Session session)
Further the pattern may be defined within an interface
which will allow you to define any additional parameters you desire. i.e.
public interface IWorkFlow
{
System.Action<bool> Invalidate { get; }
}
This will allow you to decouple the view logic with the validation logic and not have to deal with the existing mechanisms or their API paradigms.
Reguards
Upvotes: 1