By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,464 Members | 1,598 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,464 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.SelectedInde x))
{
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.SelectedIn dex))
{
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.SelectedIn dex))
{
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.Sele ctedIndex))
{
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.Sele ctedIndex))
{
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.Sele ctedIndex))
{
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.Sele ctedIndex))
{
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

Feb 4 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hello,

Try using "\r\n".

Regards
Bjorn.

Feb 4 '07 #2

P: n/a
On 4 Feb 2007 01:15:59 -0800, "Wayne Deleersnyder"
<wa***@digitalmeister.comwrote:

[snip]

Try Environment.NewLine

rossum

Feb 4 '07 #3

P: n/a
On Feb 4, 5:48 am, rostercapt...@gmail.com wrote:
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.

Feb 6 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.