NealR
NealR

Reputation: 10709

Add multi-line jquery alert

I want to concatenate a string to display in a jquery alert. I've followed the instructions, which seem pretty basic, on posts such as this, however I'm not having any luck.

For example, this jquery code

    //Validate field values
    var errors = "";
    if (inputArray[0].length != 8) {
        errors += "Life Master must be 8 digits"+'\\n';
    }
    if (inputArray[1].length != 3) {
        errors += "Market Segment must be 3 digist" + '\\n';
    }
    if (ValidateDate(inputArray[3])) {
        errors += "Invalid Effective Date" + '\\n';
    }
    if (ValidateDate(inputArray[4])) {
        errors += "Invalid Cancel Date";
    }

    if (errors != "") {
        errors = "Please check the following:" + '\\n' + errors;
        alert(errors);
        return false;
    }

creates an alert that looks like this:

enter image description here

Upvotes: 1

Views: 1559

Answers (3)

user1595702
user1595702

Reputation: 301

As others have you mentioned, you are escaping the first "\". One stylistic consideration you may want to make is to use an array that holds all of the error messages. Check if you have any errors and then join the array elements with the newline and alert the error messages.

Here is a working example that should get the point across.

jsfiddle

Upvotes: 1

Jacek Pietal
Jacek Pietal

Reputation: 2019

\n is newline

by calling \\ you are escaping \ therefore asking browser to print \n not line break

Upvotes: 1

l2aelba
l2aelba

Reputation: 22177

Just \n not \\n

like alert('one\ntwo')

Upvotes: 3

Related Questions