Validators along with Javascript (On Button Click)

  • Thread starter msnews.microsoft.com
  • Start date
M

msnews.microsoft.com

Hi

I have a question about using using Validators along with the javascript for
validations

I have a TextBox control, which i have to check for blank. I am using
RequiredFieldValidator for the same.
I have other 2 checkboxes and a Button called cmdSave for which i have added
a javascript function
cmdSave.Attributes.Add("OnClick", "return CallOnSave();")

function CallOnSave()
{
if (document.getElementById("chkCode").checked == false )
(
if (confirm("WARNING\n\nAre you sure you want to uncheck the
Code?\n\n") )
return true
else
{
return false
}
}
}

If the checkbox check is removed it gives alert and asks from Confirm()
Now when my checkbox is removed and the textBox is blank it just checks for
the checkbox neglecting the RequiredFieldValidator

HTML which is creates will have some thing like this
onclick="return CallOnSave();WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions( etc etc )))"
So I tried using
cmdSave.Attributes.Add("OnClick", "if ( CallOnSave() )")

I tried
cmdSave.Attributes.Add("OnMouseClick", "return CallOnSave();")
cmdSave.Attributes.Add("OnMouseDown", "return CallOnSave();")

I can not use CustomValidators are I can not write it on a button.

----- In short I want to run both Javascript and Validators at the same
time, and it should stop the further execution of the page. ----------

Still its not working.

Please help if you can !!
 
S

Steven Cheng[MSFT]

Hi mikrofax,

Welcome to ASPNET newsgroup.
From your description, on your asp.net webpage, you are using both a
RequiredFieldValidator and some clientside scripts to validate a certain
TextBox's value and the clientscript will continue or suspend the postback
depend on another checkbox's status. However, you're ecountering some
problem on making them work together, yes?

Based on my experience, for such scenario, it's better that we avoid using
both validation control(clientside validation) and custom script valiation
since the Validation control's clientside validation is injected in the
certain event before post back at client side and it's difficult for us to
adjust them so as to make sure they works well with our custom script. So
if possible, I'd suggest you remove the RequiredFieldVAlidator and put the
empty validation in your custom script also.

Also, if you do need to mix them, we can do it through the following trick,
here is a test page I've made to demostrate the means:

========aspx ================
<HTML>
<HEAD>
<title>scriptvalidate</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="jscript">
function custom_validate()
{
if(document.getElementById("chkSave").checked == false)
{
if (confirm("WARNING\n\nAre you sure you want to uncheck the
Code?\n\n") )
{
return true;
}
else
{
return false;
}
}
}
</script>

<script runat="server" language="C#" >
protected void Page_Load(object sender, System.EventArgs e)
{

btnSubmit.Attributes["onclick"] = "if(custom_validate() == false){return
false;};";
}

protected void btnSubmit_Click(object sender, System.EventArgs e)
{
Response.Write("PostBack_" + DateTime.Now.Ticks);
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:TextBox id="txtValue" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="rfvValue" runat="server"
ErrorMessage="RequiredFieldValidator"
ControlToValidate="txtValue"></asp:RequiredFieldValidator><BR>
<asp:CheckBox id="chkSave" runat="server"
Text="Save"></asp:CheckBox><BR>
<asp:Button id="btnSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_Click"></asp:Button>
</form>
</body>
</HTML>
==========================

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| From: "msnews.microsoft.com" <[email protected]>
| Subject: Validators along with Javascript (On Button Click)
| Date: Tue, 13 Sep 2005 13:13:21 +0100
| Lines: 49
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: 217.205.201.142
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10811
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hi
|
| I have a question about using using Validators along with the javascript
for
| validations
|
| I have a TextBox control, which i have to check for blank. I am using
| RequiredFieldValidator for the same.
| I have other 2 checkboxes and a Button called cmdSave for which i have
added
| a javascript function
| cmdSave.Attributes.Add("OnClick", "return CallOnSave();")
|
| function CallOnSave()
| {
| if (document.getElementById("chkCode").checked == false )
| (
| if (confirm("WARNING\n\nAre you sure you want to uncheck the
| Code?\n\n") )
| return true
| else
| {
| return false
| }
| }
| }
|
| If the checkbox check is removed it gives alert and asks from Confirm()
| Now when my checkbox is removed and the textBox is blank it just checks
for
| the checkbox neglecting the RequiredFieldValidator
|
| HTML which is creates will have some thing like this
| onclick="return CallOnSave();WebForm_DoPostBackWithOptions(new
| WebForm_PostBackOptions( etc etc )))"
| So I tried using
| cmdSave.Attributes.Add("OnClick", "if ( CallOnSave() )")
|
| I tried
| cmdSave.Attributes.Add("OnMouseClick", "return CallOnSave();")
| cmdSave.Attributes.Add("OnMouseDown", "return CallOnSave();")
|
| I can not use CustomValidators are I can not write it on a button.
|
| ----- In short I want to run both Javascript and Validators at the same
| time, and it should stop the further execution of the page. ----------
|
| Still its not working.
|
| Please help if you can !!
|
|
|
 
M

msnews.microsoft.com

Hi

Thanks Steve

That thing worked.
I got the same thing using
form1.Attributes.Add("OnSubmit", "return CallOnSave();")
which worked well ... But this might not be a good solution if I have more
than one buttons or some dropdown box which post back to the same page..

And one more thing ....
thanks for the regular expression solution.

^(\d{1,3}(\,\d{3})*|(\d+))(\.\d{0,2})?$
I think this will work with the set of 3 integers at one point of time ..
like big numeric price fields, which i didnt realise ... of course I was
putting comma "," internally through javascript code once the number is
completely typed...

Thanks again

Dev
MikroFax



Steven Cheng said:
Hi mikrofax,

Welcome to ASPNET newsgroup.
From your description, on your asp.net webpage, you are using both a
RequiredFieldValidator and some clientside scripts to validate a certain
TextBox's value and the clientscript will continue or suspend the postback
depend on another checkbox's status. However, you're ecountering some
problem on making them work together, yes?

Based on my experience, for such scenario, it's better that we avoid using
both validation control(clientside validation) and custom script valiation
since the Validation control's clientside validation is injected in the
certain event before post back at client side and it's difficult for us to
adjust them so as to make sure they works well with our custom script. So
if possible, I'd suggest you remove the RequiredFieldVAlidator and put the
empty validation in your custom script also.

Also, if you do need to mix them, we can do it through the following
trick,
here is a test page I've made to demostrate the means:

========aspx ================
<HTML>
<HEAD>
<title>scriptvalidate</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="jscript">
function custom_validate()
{
if(document.getElementById("chkSave").checked == false)
{
if (confirm("WARNING\n\nAre you sure you want to uncheck the
Code?\n\n") )
{
return true;
}
else
{
return false;
}
}
}
</script>

<script runat="server" language="C#" >
protected void Page_Load(object sender, System.EventArgs e)
{

btnSubmit.Attributes["onclick"] = "if(custom_validate() == false){return
false;};";
}

protected void btnSubmit_Click(object sender, System.EventArgs e)
{
Response.Write("PostBack_" + DateTime.Now.Ticks);
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:TextBox id="txtValue" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator id="rfvValue" runat="server"
ErrorMessage="RequiredFieldValidator"
ControlToValidate="txtValue"></asp:RequiredFieldValidator><BR>
<asp:CheckBox id="chkSave" runat="server"
Text="Save"></asp:CheckBox><BR>
<asp:Button id="btnSubmit" runat="server" Text="Submit"
OnClick="btnSubmit_Click"></asp:Button>
</form>
</body>
</HTML>
==========================

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



--------------------
| From: "msnews.microsoft.com" <[email protected]>
| Subject: Validators along with Javascript (On Button Click)
| Date: Tue, 13 Sep 2005 13:13:21 +0100
| Lines: 49
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: 217.205.201.142
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10811
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hi
|
| I have a question about using using Validators along with the javascript
for
| validations
|
| I have a TextBox control, which i have to check for blank. I am using
| RequiredFieldValidator for the same.
| I have other 2 checkboxes and a Button called cmdSave for which i have
added
| a javascript function
| cmdSave.Attributes.Add("OnClick", "return CallOnSave();")
|
| function CallOnSave()
| {
| if (document.getElementById("chkCode").checked == false )
| (
| if (confirm("WARNING\n\nAre you sure you want to uncheck the
| Code?\n\n") )
| return true
| else
| {
| return false
| }
| }
| }
|
| If the checkbox check is removed it gives alert and asks from Confirm()
| Now when my checkbox is removed and the textBox is blank it just checks
for
| the checkbox neglecting the RequiredFieldValidator
|
| HTML which is creates will have some thing like this
| onclick="return CallOnSave();WebForm_DoPostBackWithOptions(new
| WebForm_PostBackOptions( etc etc )))"
| So I tried using
| cmdSave.Attributes.Add("OnClick", "if ( CallOnSave() )")
|
| I tried
| cmdSave.Attributes.Add("OnMouseClick", "return CallOnSave();")
| cmdSave.Attributes.Add("OnMouseDown", "return CallOnSave();")
|
| I can not use CustomValidators are I can not write it on a button.
|
| ----- In short I want to run both Javascript and Validators at the same
| time, and it should stop the further execution of the page. ----------
|
| Still its not working.
|
| Please help if you can !!
|
|
|
 
S

Steven Cheng[MSFT]

You're welcome Midro,

Have a good day!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "msnews.microsoft.com" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Validators along with Javascript (On Button Click)
| Date: Wed, 14 Sep 2005 13:59:42 +0100
| Lines: 190
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: 217.205.201.142
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:10829
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| Hi
|
| Thanks Steve
|
| That thing worked.
| I got the same thing using
| form1.Attributes.Add("OnSubmit", "return CallOnSave();")
| which worked well ... But this might not be a good solution if I have
more
| than one buttons or some dropdown box which post back to the same page..
|
| And one more thing ....
| thanks for the regular expression solution.
|
| ^(\d{1,3}(\,\d{3})*|(\d+))(\.\d{0,2})?$
| I think this will work with the set of 3 integers at one point of time ..
| like big numeric price fields, which i didnt realise ... of course I was
| putting comma "," internally through javascript code once the number is
| completely typed...
|
| Thanks again
|
| Dev
| MikroFax
|
|
|
| | > Hi mikrofax,
| >
| > Welcome to ASPNET newsgroup.
| > From your description, on your asp.net webpage, you are using both a
| > RequiredFieldValidator and some clientside scripts to validate a certain
| > TextBox's value and the clientscript will continue or suspend the
postback
| > depend on another checkbox's status. However, you're ecountering some
| > problem on making them work together, yes?
| >
| > Based on my experience, for such scenario, it's better that we avoid
using
| > both validation control(clientside validation) and custom script
valiation
| > since the Validation control's clientside validation is injected in the
| > certain event before post back at client side and it's difficult for us
to
| > adjust them so as to make sure they works well with our custom script.
So
| > if possible, I'd suggest you remove the RequiredFieldVAlidator and put
the
| > empty validation in your custom script also.
| >
| > Also, if you do need to mix them, we can do it through the following
| > trick,
| > here is a test page I've made to demostrate the means:
| >
| > ========aspx ================
| > <HTML>
| > <HEAD>
| > <title>scriptvalidate</title>
| > <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
| > <meta name="CODE_LANGUAGE" Content="C#">
| > <meta name="vs_defaultClientScript" content="JavaScript">
| > <meta name="vs_targetSchema"
| > content="http://schemas.microsoft.com/intellisense/ie5">
| > <script language="jscript">
| > function custom_validate()
| > {
| > if(document.getElementById("chkSave").checked == false)
| > {
| > if (confirm("WARNING\n\nAre you sure you want to uncheck the
| > Code?\n\n") )
| > {
| > return true;
| > }
| > else
| > {
| > return false;
| > }
| > }
| > }
| > </script>
| >
| > <script runat="server" language="C#" >
| > protected void Page_Load(object sender, System.EventArgs e)
| > {
| >
| > btnSubmit.Attributes["onclick"] = "if(custom_validate() == false){return
| > false;};";
| > }
| >
| > protected void btnSubmit_Click(object sender, System.EventArgs e)
| > {
| > Response.Write("PostBack_" + DateTime.Now.Ticks);
| > }
| > </script>
| > </HEAD>
| > <body>
| > <form id="Form1" method="post" runat="server">
| > <asp:TextBox id="txtValue" runat="server"></asp:TextBox>
| > <asp:RequiredFieldValidator id="rfvValue" runat="server"
| > ErrorMessage="RequiredFieldValidator"
| > ControlToValidate="txtValue"></asp:RequiredFieldValidator><BR>
| > <asp:CheckBox id="chkSave" runat="server"
| > Text="Save"></asp:CheckBox><BR>
| > <asp:Button id="btnSubmit" runat="server" Text="Submit"
| > OnClick="btnSubmit_Click"></asp:Button>
| > </form>
| > </body>
| > </HTML>
| > ==========================
| >
| > Hope helps. Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| >
| >
| >
| > --------------------
| > | From: "msnews.microsoft.com" <[email protected]>
| > | Subject: Validators along with Javascript (On Button Click)
| > | Date: Tue, 13 Sep 2005 13:13:21 +0100
| > | Lines: 49
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Original
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| > | NNTP-Posting-Host: 217.205.201.142
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet.webcontrols:10811
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
| > |
| > | Hi
| > |
| > | I have a question about using using Validators along with the
javascript
| > for
| > | validations
| > |
| > | I have a TextBox control, which i have to check for blank. I am using
| > | RequiredFieldValidator for the same.
| > | I have other 2 checkboxes and a Button called cmdSave for which i have
| > added
| > | a javascript function
| > | cmdSave.Attributes.Add("OnClick", "return CallOnSave();")
| > |
| > | function CallOnSave()
| > | {
| > | if (document.getElementById("chkCode").checked == false )
| > | (
| > | if (confirm("WARNING\n\nAre you sure you want to uncheck the
| > | Code?\n\n") )
| > | return true
| > | else
| > | {
| > | return false
| > | }
| > | }
| > | }
| > |
| > | If the checkbox check is removed it gives alert and asks from
Confirm()
| > | Now when my checkbox is removed and the textBox is blank it just
checks
| > for
| > | the checkbox neglecting the RequiredFieldValidator
| > |
| > | HTML which is creates will have some thing like this
| > | onclick="return CallOnSave();WebForm_DoPostBackWithOptions(new
| > | WebForm_PostBackOptions( etc etc )))"
| > | So I tried using
| > | cmdSave.Attributes.Add("OnClick", "if ( CallOnSave() )")
| > |
| > | I tried
| > | cmdSave.Attributes.Add("OnMouseClick", "return CallOnSave();")
| > | cmdSave.Attributes.Add("OnMouseDown", "return CallOnSave();")
| > |
| > | I can not use CustomValidators are I can not write it on a button.
| > |
| > | ----- In short I want to run both Javascript and Validators at the
same
| > | time, and it should stop the further execution of the page. ----------
| > |
| > | Still its not working.
| > |
| > | Please help if you can !!
| > |
| > |
| > |
| >
|
|
|
 

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,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top