is there a way to perform a client side kind of Page.IsValid validation?

M

Mike Chen

We know in server code, we can check the page validated by using
Page.IsValid where we put some validator controls on aspx page.
I want to set some value after validating user input values on client
side and before page posts to server. How can i get the functionality of
client side Page.IsValid? thanks.
 
M

Mark Rae

We know in server code, we can check the page validated by using
Page.IsValid where we put some validator controls on aspx page.
I want to set some value after validating user input values on client side
and before page posts to server. How can i get the functionality of client
side Page.IsValid? thanks.

I have my own set of JavaScript routines for this.

I don't use the validator controls at all, unless I absolutely have to (e.g.
for custom controls), as I find them incredibly cumbersome and restrictive.
 
G

Guest

Hi Mark and Mike,

Of course there is. ASP.NET provides nice client side API to support custom,
non standard scenarios. In addtion to that Mark, there is no need for coding
such functionality on your own as provided API can solve 99% cases. Let me
give you a simple example how to validate the page youself using client side
API:

<asp:TextBox runat="server" ID="txtName"/>
<asp:RequiredFieldValidator runat="server" ID="rfvName"
ControlToValidate="txtName" ErrorMessage="Got ya!" EnableClientScript="true"/>
<asp:Button runat="server" ID="btnSubmit" CausesValidation="false"
Text="Submit!" OnClientClick="return ValidateOrShowDialogBox2()"/>

<script type="text/javascript">


// example that checks particular validator
function ValidateOrShowDialogBox()
{
var validator = document.getElementById('<%=rfvName.ClientID %>');

ValidatorValidate(validator);

// you can use Page_Validators array instead
if (validator.isvalid)
{
// create dialog box here
alert('well done!');
}
// set it to true to post back page
return false;
}

// example that checks all validators on the page
function ValidateOrShowDialogBox2()
{
var validator;

for (var i = 0; i < Page_Validators.length; i++)
{
validator = Page_Validators;
ValidatorValidate(validator);

// validation fails if at least one validator fails
if (!validator.isvalid)
return false;
}

// create dialog box here
alert('well done!');

// set it to true to post back page
return false;
}

</script>

One more thing. You may apply several validators to the same control (in
conjunction with ValidationSummary control and/or Display property of the
validator) which is a quick and efficient way of building validation chain.

Regards guys
 
M

Mark Rae

Of course there is.

I didn't say there wasn't...
In addtion to that Mark, there is no need for coding such functionality on
your own

Yes there is - the need is the fact that that's the way I choose to do it...
Let me give you a simple example

ROTFLMAO!!! And that's *precisely* why I roll my own validation - thanks
very much , you really made my day...:)
 
G

Guest

Mark,

don't take it personally ;-) My reply is based on Mike's question and your
reply - you didn't mention validation api so i simply thought you didn't know
too.
 
M

Mike Chen

Milosz said:
Mark,

don't take it personally ;-) My reply is based on Mike's question and your
reply - you didn't mention validation api so i simply thought you didn't know
too.

Thanks Milosz, it did work fine for me.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top