views:

209

answers:

5

I have a small class that displays an alert via Javascript. I am using this class on a page that checks if the user entered a valid zip code. If a valid zip code is not entered, an alert is displayed, and the page is posted back to the same page so they can change it. If they enter a valid zip code, they are redirected to another page. The problem is that if they hit the back button on their browser, the alert is displayed regardless of whether the zip code is valid or not.

Here is the flow:

Zip entry page -> Enter Zip -> Check Zip

if valid zip -> goto overview.aspx if not valid zip, goto zip entry page

On overview.aspx, if they hit their browser's back button, the alert is displayed. I don't want this to happen. Below is the code I have for the alert class which works great. And below that the code that checks and displays the alert when the LinkButton is clicked.

protected void GotoReview(object sender, EventArgs e) {
        if (ValidateZipCode(ZipCode)) {
            Response.Redirect(string.Format("~/checkout/overview.aspx?pc={0}&zc={1}",
            ProductCode, ZipCode));
        }
        else {
            Alert.Show("The entered zip code is invalid. Please ensure the zip code is a valid zip code.");
        }
    }

private bool ValidateZipCode(string zip) {
        if (zip.Length < 5 || zip.Length > 5) {
            return false;
        }
        if (!Regex.IsMatch(zip, @"^(\d{5}-\d{4})|(\d{5})$")) {
            return false;
        }
        return true;
    }

public static class Alert {
public static void Show(string message) {
    string cleanMessage = message.Replace("'", "\\'");
    string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";
    Page page = HttpContext.Current.CurrentHandler as Page;
    if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert")) {
        page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);
    }
}

}

A: 

I think the value of zipcode gets lost when you redirect (but the added script remains as it is).

Why don't you put the validation in javascript (on the page having the zipcode entry field)?
It doesn't look like, the validation needs things from server side (as you are doing length check and regex check)

shahkalpesh
A: 

I can confirm that the zip code is properly stored (Session["zc"]), and is sent to overview.aspx from zip.aspx. To simplify, when the user is on overview.aspx, and clicks the back button on their browser, that's when the Alert script executes. I tried putting a breakpoint in the debugger, but it never gets called. So the only assumption I have now is registered scripts get called when using the back button?

I don't understand why the script executes while going back though, even if the zip was valid going forward. :\

David Anderson
A: 

when you click the LinkButton browser requests the same page (zip code entry page) and then gets redirected to overview.aspx (if everything's fine). But when you click Back button, it seems that browser performs previous request - validates zip code, not displays zip code entry page.

In any case, it's better to perform such a check on client, as stated shahkalpesh

x-yuri
A: 

Back buttons work differently on all browsers, some browsers will retrieve the newest version, others will simply return what the latest on disk copy is.

The best thing to do here is probably to bust out the old javascript and add an OnSubmit to the form to check for proper validations, jquery would be a great tool in this instance.

With this method, no matter the browsers back strategy, your alert will not be called unless they click on the submit button.

Tom Anderson
A: 

Thank you very much guys, the solution seems to be to use Javascript. I'll check out this jQuery I keep hearing about everywhere too.

Thanks guys!

David Anderson