Richard77
Richard77

Reputation: 21671

How to force javascript to run after the required validation control?

I have a textbox that should not be empty, so I have placed a required validation control next to it. I have also a javascript confirmation box that asks the user is he's sure to submit the information.

The problem is that the Confirmation is appearing first, then the validation is kicking in after. I'd like to inverse that. I want the validation to proceed first. If there is no more error, then the user is asked if he's sure to submit the info.

EDIT

This the markup

<asp:LinkButton ID="_lnkAddUpdate" runat="server" CausesValidation="True" OnClientClick = "return ConfirmAddEdit(this.id)"
     CommandName="Update"></asp:LinkButton>

<asp:TextBox ID="_tbLocationName" runat="server" Text= '<%# Eval("LocationName") %>'/>
<asp:RequiredFieldValidator ID="reqLocationName" runat="server" ErrorMessage="Location name cannot be empty string." Text = "*" ControlToValidate = "_tbLocationName" />

This is the javascript

function ConfirmActive(id) {
    var action = document.getElementById(id).innerHTML;
    var r = confirm("Are you sure you want to " + action + " this location?");
    if (r == false) {
        return false;
    }
}

Thanks for helping.

Upvotes: 0

Views: 822

Answers (2)

freefaller
freefaller

Reputation: 19963

The way I normally do it is to have the following javascript...

var cont = true;
if (typeof (Page_ClientValidate) === "function") {
  cont = Page_ClientValidate();
}
if (cont) {
  cont = window.confirm("are you sure?");
}
return cont;

This will check to see if the ASP.NET function is available, and if so run it... if it's valid then the confirm is fired. The final result is sent back to the click handler as to whether to continue or not.


Update

To give a bit more explanation:

When you set the OnClientClick attribute, the javascript you set is always run first... therefore if you have any validator controls in your page, they will only be run after your javascript. This is obviously not what you want.

What my solution does is to call the exact same function that would be called by the browser after your javascript, but before. The minor downside is that the validation will be carried out twice (one directly by you, and then again after your function has run).

function ConfirmActive(id) {
  var cont = true;
  if (typeof (Page_ClientValidate) === "function") {
    cont = Page_ClientValidate();
  }
  if (cont) {
    var action = document.getElementById(id).innerHTML;
    cont = confirm("Are you sure you want to " + action + " this location?");
  }
  return cont
}

Upvotes: 1

hobberwickey
hobberwickey

Reputation: 6444

Without seeing the code it's impossible to know for sure what the problem is, but I'd guess you're having one of two problems.

The first thing to check is that your validation code is before your confirmation code

validate();
confirm();

Easy enough. The second and I'm guess your actual problem is that you're using remote validation, so instead of the above solution what you'd want is to make your confirmation code be a callback to your validation code. How you do that depends on how you're setting up the validation request. Post that code and we can help you out.

Upvotes: 1

Related Questions