Run client side javascript with Validation Control

G

goscottie

I need to find a way to either

1. run and check all Validation controls (in my case one
CompareValidator) and run client side javascript function. In this
case I'll use <asp:Button>. So if all validation passes, run custom
javascript. Or.
2. from my client side javascript to run Validation check and then
perform remaining client side function. So in this case I'll use
<input type="button">.

So far, challenge in case 1 is how to capture validation failure and
limit and stop further client side code execution. I had this code.

Button_Search.Attributes.Add("onclick",
Page.ClientScript.GetPostBackEventReference(Button_Search, "") +
";Search();");

where Search() is my client side function, which gets executed with
failed client side validation.

In case 2, I would like to do something like... (I'm doing this with
RegisterClientScriptBlock)

function Search()
{
window.top._somevalue = 'xyz';
if (whateverclientvalidationfuctioniscalled()) // client side
validation function name generated by ASP.NET
window.top.hidePopWin();
}

<input type="button" onclick="Search();">

Any help appreciated.
 
P

Pselus

I think you are trying to do what I do. Here is what I do:

In the code-behind:

// Add Submit Confirmation
btnSubmit.Attributes.Add("onClick", "return ConfirmSubmit();");

Then in your aspx page within script tags:

function errorArgs()
{
this.IsValid = true;
this.errorMsg = "";
}

function ConfirmSubmit()
{
var IsValid = true;
var display = "";
var err = new errorArgs();

VerifyVacationDays(err); // Create your validation code in here.

// I have multiple validation routines that I call, but I put it
down to 1 for ease of reading

if (err.IsValid)
{
return confirm('Are you sure you want to Submit?');
}
else
{
alert(err.errorMsg);
return false;
}
}

The key is the "return" in the buttons OnClick event. If it sees
false it will not post back. If it sees true it will.
 
G

goscottie

Thanks for quick response.

So looks like you are calling your own validation functions. I would
like to utilize already generated server-side validation. I just
found my own answer just after posting.

http://forums.asp.net/t/1318892.aspx

With case 2, where...

if (Page_ClientValidate('SearchValidation')) // that was the function
name I was looking for. SearchValidation is the ValidationGroup name.
window.top.hidePopWin();

and used in server side....

<asp:Button ID="Button_Search" runat="server" Text="Search"
ValidationGroup="SearchValidation" OnClientClick="Search();" />

I'm not sure <asp:Button> is completely necessary or not here but I'll
move on. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top