Validation Controls and OnClientClick

J

Jennifer Mathews

In a form
<asp:ValidationSummary ID="rfvg_Submit" runat="server" />

<asp:TextBox ID="txt_Last_Name" Text="test" runat="server" AutoPostBack="False"
EnableViewState="True" MaxLength="30" />
<asp:RequiredFieldValidator ID="rfv_txt_Last_Name" runat="server"
ControlToValidate="txt_Last_Name" ErrorMessage="whatever" ValidationGroup="rfvg_Submit"
CssClass="ReqrFldValidator_Err_Msg_RghtOfCtl" InitialValue="" Text="*"
SetFocusOnError="True" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="rev_txt_Last_Name" runat="server"
ControlToValidate="txt_Last_Name" ValidationGroup="rfvg_Submit" InitialValue="" Text="*"
SetFocusOnError="True" Display="Dynamic" ErrorMessage="Last Name length is between 2 &
30 characters and can alphanumeric and spaces." ValidationExpression="[
0-9a-zA-Z]{2,30}" />

<asp:ImageButton ID="cmdSave" runat="server" ImageAlign="Middle" ImageUrl="Save.jpg"
ValidationGroup="rfvg_Submit" />

When the user press <SAVE> button it properly validates and doesn't submit itself to the
code-behind. Yeah.

But when I put OnClientClick in the <SAVE> button there is a problem:

<asp:ImageButton ID="cmdSave" runat="server" ImageAlign="Middle" ImageUrl="Save.jpg"
ValidationGroup="rfvg_Submit" OnClientClick="return Validat_cmdSave();" />

JAVASCRIPT
function Validat_cmdSave()
{
var isGood = true;
if (whatever == 1)
{
isGood = false;
}
return isGood;
}

If the JavaScript returns FALSE, the form does NOT post back as expect.

If their is an error in the RequiredFieldValidator or RegularExpressionValidator
validator and
the JavaScript returns TRUE, the form still posts back to the code-beind. I do not want
it
to post back to the code-behind though.

Is there a solution to this?

Thanks
 
N

Nathan Sokalski

First of all, why are you using OnClientClick and the ASP.NET Validators to
validate your form? Perhaps if you explain your reason(s) for doing this we
can help more by suggesting the correct way to do it.
 
B

bruce barker

as you do a return in either case (valid/not valid), the standard
validation logic is not called as it comes after your client script.
change it to:

OnClientClick="if (!Validat_cmdSave()) return false;"

a quick look at the page source should have told you this.

-- bruce (sqlwork.com)
 
J

Jennifer Mathews

That did it. Thanks a ton Bruce. I banged my head against the wall and did search the
internet for an answer.
No examples showed what you showed in one line of code.

Thanks again

bruce barker said:
as you do a return in either case (valid/not valid), the standard validation logic is
not called as it comes after your client script. change it to:

OnClientClick="if (!Validat_cmdSave()) return false;"

a quick look at the page source should have told you this.

-- bruce (sqlwork.com)

Jennifer said:
In a form
<asp:ValidationSummary ID="rfvg_Submit" runat="server" />

<asp:TextBox ID="txt_Last_Name" Text="test" runat="server" AutoPostBack="False"
EnableViewState="True" MaxLength="30" />
<asp:RequiredFieldValidator ID="rfv_txt_Last_Name" runat="server"
ControlToValidate="txt_Last_Name" ErrorMessage="whatever"
ValidationGroup="rfvg_Submit" CssClass="ReqrFldValidator_Err_Msg_RghtOfCtl"
InitialValue="" Text="*" SetFocusOnError="True"
Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="rev_txt_Last_Name" runat="server"
ControlToValidate="txt_Last_Name" ValidationGroup="rfvg_Submit" InitialValue=""
Text="*" SetFocusOnError="True" Display="Dynamic" ErrorMessage="Last Name length is
between 2 & 30 characters and can alphanumeric and spaces." ValidationExpression="[
0-9a-zA-Z]{2,30}" />

<asp:ImageButton ID="cmdSave" runat="server" ImageAlign="Middle" ImageUrl="Save.jpg"
ValidationGroup="rfvg_Submit" />

When the user press <SAVE> button it properly validates and doesn't submit itself to
the code-behind. Yeah.

But when I put OnClientClick in the <SAVE> button there is a problem:

<asp:ImageButton ID="cmdSave" runat="server" ImageAlign="Middle" ImageUrl="Save.jpg"
ValidationGroup="rfvg_Submit" OnClientClick="return Validat_cmdSave();" />

JAVASCRIPT
function Validat_cmdSave()
{
var isGood = true;
if (whatever == 1)
{
isGood = false;
}
return isGood;
}

If the JavaScript returns FALSE, the form does NOT post back as expect.

If their is an error in the RequiredFieldValidator or RegularExpressionValidator
validator and
the JavaScript returns TRUE, the form still posts back to the code-beind. I do not
want it
to post back to the code-behind though.

Is there a solution to this?

Thanks
 
R

Raghupathi K

In a form
<asp:ValidationSummary ID="rfvg_Submit" runat="server" />

<asp:TextBox ID="txt_Last_Name" Text="test" runat="server" AutoPostBack="False"
EnableViewState="True" MaxLength="30" />
<asp:RequiredFieldValidator ID="rfv_txt_Last_Name" runat="server"
ControlToValidate="txt_Last_Name" ErrorMessage="whatever" ValidationGroup="rfvg_Submit"
CssClass="ReqrFldValidator_Err_Msg_RghtOfCtl" InitialValue="" Text="*"
SetFocusOnError="True" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="rev_txt_Last_Name" runat="server"
ControlToValidate="txt_Last_Name" ValidationGroup="rfvg_Submit" InitialValue="" Text="*"
SetFocusOnError="True" Display="Dynamic" ErrorMessage="Last Name length is between 2 &
30 characters and can alphanumeric and spaces." ValidationExpression="[
0-9a-zA-Z]{2,30}" />

<asp:ImageButton ID="cmdSave" runat="server" ImageAlign="Middle" ImageUrl="Save.jpg"
ValidationGroup="rfvg_Submit" />

When the user press <SAVE> button it properly validates and doesn't submit itself to the
code-behind.  Yeah.

But when I put OnClientClick in the <SAVE> button there is a problem:

<asp:ImageButton ID="cmdSave" runat="server" ImageAlign="Middle" ImageUrl="Save.jpg"
ValidationGroup="rfvg_Submit" OnClientClick="return Validat_cmdSave();" />

JAVASCRIPT
function Validat_cmdSave()
{
    var isGood = true;
    if (whatever == 1)
    {
        isGood = false;
    }
    return isGood;

}

If the JavaScript returns FALSE, the form does NOT post back as expect.

If their is an error in the RequiredFieldValidator or RegularExpressionValidator
validator and
the JavaScript returns TRUE, the form still posts back to the code-beind.  I do not want
it
to post back to the code-behind though.

Is there a solution to this?

Thanks

Where is your "whatever" variable defined.... ??
 
J

Jennifer Mathews

"whatever" is not a variable, I just didn't want to include the ACTUAL ErrorMessage in
this message to cut down on the clutter.

In a form
<asp:ValidationSummary ID="rfvg_Submit" runat="server" />

<asp:TextBox ID="txt_Last_Name" Text="test" runat="server" AutoPostBack="False"
EnableViewState="True" MaxLength="30" />
<asp:RequiredFieldValidator ID="rfv_txt_Last_Name" runat="server"
ControlToValidate="txt_Last_Name" ErrorMessage="whatever"
ValidationGroup="rfvg_Submit"
CssClass="ReqrFldValidator_Err_Msg_RghtOfCtl" InitialValue="" Text="*"
SetFocusOnError="True" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="rev_txt_Last_Name" runat="server"
ControlToValidate="txt_Last_Name" ValidationGroup="rfvg_Submit" InitialValue=""
Text="*"
SetFocusOnError="True" Display="Dynamic" ErrorMessage="Last Name length is between 2 &
30 characters and can alphanumeric and spaces." ValidationExpression="[
0-9a-zA-Z]{2,30}" />

<asp:ImageButton ID="cmdSave" runat="server" ImageAlign="Middle" ImageUrl="Save.jpg"
ValidationGroup="rfvg_Submit" />

When the user press <SAVE> button it properly validates and doesn't submit itself to
the
code-behind. Yeah.

But when I put OnClientClick in the <SAVE> button there is a problem:

<asp:ImageButton ID="cmdSave" runat="server" ImageAlign="Middle" ImageUrl="Save.jpg"
ValidationGroup="rfvg_Submit" OnClientClick="return Validat_cmdSave();" />

JAVASCRIPT
function Validat_cmdSave()
{
var isGood = true;
if (whatever == 1)
{
isGood = false;
}
return isGood;

}

If the JavaScript returns FALSE, the form does NOT post back as expect.

If their is an error in the RequiredFieldValidator or RegularExpressionValidator
validator and
the JavaScript returns TRUE, the form still posts back to the code-beind. I do not
want
it
to post back to the code-behind though.

Is there a solution to this?

Thanks

Where is your "whatever" variable defined.... ??
 

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,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top