Jump to content

display one alert in javascript

Go to solution Solved by Blade of Grass,

You need to created a state machine for this. Use one variable to track if it's fired and then check that before displaying the alert.

var hasFired = false;

// procedure for the rest of the code

// firing procedure
if (!hasFired) {
    fireAlert();
    hasFired = true;
}

 

edit, sorry, didn't read your other message. 

 

I would suggest you build a "Msg" variable that holds your entire alert message and fire it once. 

var msg = "";
If (cond1) {
     Msg = Msg + "Something is wrong! ";
}

if (cond2) {
     Msg = msg + " Something else is wrong!";
}

if (msg != "") {
     alert(msg);
}

 

i have multiple instances of alert in my javascript if the user has input an error or invalid statement the alert will pop out, when there are multiple instances of error the web page shows multiple alert messages how can i filter it to just one alert message even if there are multiple incorrect inputs.

Link to comment
Share on other sites

Link to post
Share on other sites

Do you mean some own popups or the "alert()" function? Please give us the code, we can't know what the issue is if we don't know how it works.

Link to comment
Share on other sites

Link to post
Share on other sites

if((sag!=0)&&(mmmon="FEBRUARY")&&(cont==29)){
alert("Invalid");
location.reload();
}               this is part of the code which filters the leapyear input of the user

 

else if((mmmon=="FEBRUARY")&&(sam>=31)){
alert("Invalid");                            this the code if the user's input is not valid or over valued 
location.reload();
}

 

 

if((mod<1962)||(mod>2009)||(mod-mod!=0)){
alert("Invalid Input");                             part of the code if the user is out of bound or not valid or not a number
      location.reload();
}
if((mmmon=="JANUARY")&&(sam>=32)){
alert("Invalid");                                     input out of bound
location.reload();
}

 

 

 

if i entered nothing in the forms the program will send a alert() but it shows multiple alerts because theres no input its out of bound and not equal to 0 so there is 3 instances of alert i want it to only have 1 alert even if there are multiple errors 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

You need to created a state machine for this. Use one variable to track if it's fired and then check that before displaying the alert.

var hasFired = false;

// procedure for the rest of the code

// firing procedure
if (!hasFired) {
    fireAlert();
    hasFired = true;
}

 

edit, sorry, didn't read your other message. 

 

I would suggest you build a "Msg" variable that holds your entire alert message and fire it once. 

var msg = "";
If (cond1) {
     Msg = Msg + "Something is wrong! ";
}

if (cond2) {
     Msg = msg + " Something else is wrong!";
}

if (msg != "") {
     alert(msg);
}

 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×