Validation Controls and OnClientClick

  • Thread starter Thread starter Jennifer Mathews
  • Start date Start date
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
 
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.
 
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)
 
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
 
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.... ??
 
"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.... ??
 
Back
Top