Unable to get line-breaks for a JavaScript 'alert' message using ClientScript.RegisterStartUpScript

  • Thread starter Wayne Deleersnyder
  • Start date
W

Wayne Deleersnyder

Hi All,

I'm trying to create a function that will cause a pop-up alert to
appear if dates which were chosen from a drop-down list were invalid
on a page. There's 4 dates, so there's the possibility of 4 errors at
once. I'd like to pop-up all the errors at once, so the user can
correct all the errors at once. My issue is format. I'd like a
message something like...

ERROR: Acquired Date Invalid - only 28 days in February for year
chosen
ERROR: Disposition Date Invalid - only 30 days in the month, or wrong
month chosen
ERROR: License Expiration Date Invalid - only 29 days in February for
year chosen
ERROR: Support Expiration Date Invalid - only 28 days in February for
year chosen

.... that's using line breaks. In normal JavaScript I'd do something
like this...
alert("Hello again! This is how we" + '\n' + "add line breaks to an
alert box!")

.... however, for some reason I can't get the escape sequence to
properly generate the line breaks. So all the error messages get
mushed together. I've tried adding the '\n' in a multitude of ways,
but it always gets messed. Most of the time it just won't work at
all... with the pop-up that is. It'll still return the proper boolean
value, just no pop-up alert. Has anyone else run across this issue
and solved it? Is there a better way of doing this? Any help or
insight would be appreciated.

I'm posting my code below so that you know what I'm talking about :)


// informationIsValid()
// Description: goes through the information entered to make
sure it's valid.
// If it's valid, then the information can be submitted to the
database. If not,
// an error message is displayed
protected bool informationIsValid()
{
string alertMessage = "";
string alertScript;
bool isValid = true;

try
{
// check if Acquired Date is valid
if (ddl_AcquiredDay.SelectedIndex > 29 &&
ddl_AcquiredMonth.SelectedIndex == 2 &&
DateTime.IsLeapYear(ddl_AcquiredYear.SelectedIndex))
{
alertMessage += "ERROR: Acquired Date Invalid - only
29 days in February for year chosen";
isValid = false;
}

if (ddl_AcquiredDay.SelectedIndex > 28 &&
ddl_AcquiredMonth.SelectedIndex == 2 &&
!DateTime.IsLeapYear(ddl_AcquiredYear.SelectedIndex))
{
alertMessage += "ERROR: Acquired Date Invalid - only
28 days in February for year chosen";
isValid = false;
}


// April =4, June = 6, Sept = 9, Nov = 11... these
months only have 30 days to them
if ((ddl_AcquiredMonth.SelectedIndex == 4 ||
ddl_AcquiredMonth.SelectedIndex == 6 ||
ddl_AcquiredMonth.SelectedIndex == 9 ||
ddl_AcquiredMonth.SelectedIndex == 11) &&
ddl_AcquiredDay.SelectedIndex > 30)
{
alertMessage += "ERROR: Acquired Date Invalid - only
30 days in the month, or wrong month chosen";
isValid = false;
}


// check if Dispostion Date is valid
if (ddl_DispositionDay.SelectedIndex > 29 &&
ddl_DispositionMonth.SelectedIndex == 2 &&
DateTime.IsLeapYear(ddl_DispositionYear.SelectedIndex))
{
alertMessage += "ERROR: Disposition Date Invalid -
only 29 days in February for year chosen";
isValid = false;
}

if (ddl_DispositionDay.SelectedIndex > 28 &&
ddl_DispositionMonth.SelectedIndex == 2 &&
!
DateTime.IsLeapYear(ddl_DispositionYear.SelectedIndex))
{
alertMessage += "ERROR: Disposition Date Invalid -
only 28 days in February for year chosen";
isValid = false;
}


if ((ddl_DispositionMonth.SelectedIndex == 4 ||
ddl_DispositionMonth.SelectedIndex == 6 ||
ddl_DispositionMonth.SelectedIndex == 9 ||
ddl_DispositionMonth.SelectedIndex == 11) &&
ddl_DispositionDay.SelectedIndex > 30)
{
alertMessage += "ERROR: Disposition Date Invalid -
only 30 days in the month, or wrong month chosen";
isValid = false;
}


if (ddl_LicenseExpirationDay.SelectedIndex > 29 &&
ddl_LicenseExpirationMonth.SelectedIndex == 2 &&

DateTime.IsLeapYear(ddl_LicenseExpirationYear.SelectedIndex))
{
alertMessage += "ERROR: License Expiration Date
Invalid - only 29 days in February for year chosen";
isValid = false;
}

if (ddl_LicenseExpirationDay.SelectedIndex > 28 &&
ddl_LicenseExpirationMonth.SelectedIndex == 2 &&
!
DateTime.IsLeapYear(ddl_LicenseExpirationYear.SelectedIndex))
{
alertMessage += "ERROR: License Expiration Date
Invalid - only 28 days in February for year chosen";
isValid = false;
}

if ((ddl_LicenseExpirationMonth.SelectedIndex == 4 ||
ddl_LicenseExpirationMonth.SelectedIndex == 6 ||
ddl_LicenseExpirationMonth.SelectedIndex == 9 ||
ddl_LicenseExpirationMonth.SelectedIndex == 11) &&
ddl_LicenseExpirationDay.SelectedIndex > 30)
{
alertMessage += "ERROR: License Expiration Date
Invalid - only 30 days in the month, or wrong month chosen";
isValid = false;
}



if (ddl_SupportExpirationDay.SelectedIndex > 29 &&
ddl_SupportExpirationMonth.SelectedIndex == 2 &&

DateTime.IsLeapYear(ddl_SupportExpirationYear.SelectedIndex))
{
alertMessage += "ERROR: Support Expiration Date
Invalid - only 29 days in February for year chosen";
isValid = false;
}

if (ddl_SupportExpirationDay.SelectedIndex > 28 &&
ddl_SupportExpirationMonth.SelectedIndex == 2 &&
!
DateTime.IsLeapYear(ddl_SupportExpirationYear.SelectedIndex))
{
alertMessage += "ERROR: Support Expiration Date
Invalid - only 28 days in February for year chosen";
isValid = false;
}

if ((ddl_SupportExpirationMonth.SelectedIndex == 4 ||
ddl_SupportExpirationMonth.SelectedIndex == 6 ||
ddl_SupportExpirationMonth.SelectedIndex == 9 ||
ddl_SupportExpirationMonth.SelectedIndex == 11) &&
ddl_SupportExpirationDay.SelectedIndex > 30)
{
alertMessage += "ERROR: Support Expiration Date
Invalid - only 30 days in the month, or wrong month chosen";
isValid = false;
}
}
catch (Exception err)
{
lbl_status.Text = "An error occurred: ";
lbl_status.Text += err.Message;
}

if (!isValid)
{
alertScript = "<script language=JavaScript>alert('" +
alertMessage + "')</script>";
ClientScript.RegisterStartupScript(this.GetType(), "Alert
Message", alertScript);
return false;
}
else
{
return true;
}

}

Thanks,
Wayne
 
R

rossum

On 4 Feb 2007 01:15:59 -0800, "Wayne Deleersnyder"

[snip]

Try Environment.NewLine

rossum
 
W

Wayne Deleersnyder

Hello,

Try using "\r\n".

Regards
Bjorn.

Woot! Thank Bjorn. I actually used "\\r\\n" but it did the trick.

Thanks for the help.
Wayne D.

PS - sorry for getting back so late.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top