470,580 Members | 2,204 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,580 developers. It's quick & easy.

Client Side validation and Post to method in CodeBehind Class

I have a client side Javascript which checks an OrderQuantityField against a hidden Textbox of the Minimum Order Quantity. I dont want to do validation on a postback. I would like to be able to have FormValidate Javascript function postback to my addToCart Method in my code behind

function ValidateForm(myform) {
if (myform.OrderQuantity.value == 0) {
alert("Form is Invalid = 0 " + myform.OrderQuantity.value); return false
}
if (parseInt(myform.OrderQuantity.value,10) >= parseInt(myform.MinQuantity_Hidden.value,10)) {
}
else { alert("Order Quantity must be greater than " + myform.MinQuantity_Hidden);
myform.OrderQuantity.focus() return false
}
if (parseInt(myform.OrderQuantity.value,10) % parseInt(myform.MinQuantity_Hidden.value,10))
{ alert("Order Quantity must be in multiples of " + myform.MinQuantity_Hidden.value);

myform.OrderQuantity.focus()
return false
}
//Some code here to post back to the server method??

return true
}
Nov 18 '05 #1
5 2012
If you use a CustomValidator control, you can check against the validator in
both client and server script (codeBehind). The CustomValidator will have to
be a true/false JavaScript routine (true = valid). This is the best of both
worlds, as you can stop the form from submitting, but will also prevent
hackers from posting invalid data from their own form that calls your page.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"Gary Vidal" <ga**@usballoon.net> wrote in message
news:u9****************@tk2msftngp13.phx.gbl...
I have a client side Javascript which checks an OrderQuantityField against a
hidden Textbox of the Minimum Order Quantity. I dont want to do validation
on a postback. I would like to be able to have FormValidate Javascript
function postback to my addToCart Method in my code behind

function ValidateForm(myform) {
if (myform.OrderQuantity.value == 0) {
alert("Form is Invalid = 0 " + myform.OrderQuantity.value); return false
}
if (parseInt(myform.OrderQuantity.value,10) >=
parseInt(myform.MinQuantity_Hidden.value,10)) {
}
else { alert("Order Quantity must be greater than " +
myform.MinQuantity_Hidden);
myform.OrderQuantity.focus() return false
}
if (parseInt(myform.OrderQuantity.value,10) %
parseInt(myform.MinQuantity_Hidden.value,10))
{ alert("Order Quantity must be in multiples of " +
myform.MinQuantity_Hidden.value);

myform.OrderQuantity.focus()
return false
}
//Some code here to post back to the server method??

return true
}
Nov 18 '05 #2
Hi Gary,
Thanks for posting in the community!
From your description, you use a clientside script to do some validation
operations on some certain entry fields on a web form. And after the
valication , you 'd like to post back the page via javascript code,yes?
If there is anything I misunderstood, please feel free to let me know.

As for this question, I think there are two generic approachs we can use:
1. using the "form.submit()" method to post back the whole page form.
For example, the page form is named as below in page:
<form id="Form1"....>

Then we can use the below javascript to post back the form.
document.Form1.submit();

After that, you can do some operations in the page's Page_Load handler in
codebehind.

2. adding a runat=server htmlinputbutton in the page and set its visible as
false, and use javascript to fire its click event. For example, we have
such a button in page:
<input type="button" id="btnPostBack" style="display:none" runat="server"
value="Post Back">

then, in your javascript block , call the following code to fire the
button's click event:
document.all("btnPostBack").click();

Also, since we set it as "runat=server", we can also register a server
click event handler in it (in code behind) and do serverside operations in
that handler, how do you think of this?

In addition, here is a simple test page I used to show the #2 means above,
you may have a view if you feel anything unclear:

-----------------------------aspx page----------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
<script language=javascript>
function checkField()
{
if(document.all("txtMain").value.length>4)
{
document.all("btnPostBack").click();
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<tr>
<td><INPUT id="txtMain" type="text" runat="server"
onchange="checkField()" ></td>
</tr>
<tr>
<td><INPUT id="btnPostBack" style="DISPLAY: none" type="button"
value="PostBack" runat="server"></td>
</tr>
</table>
</form>
</body>
</HTML>

-----------------------code behind class--------------------------
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputText txtMain;
protected System.Web.UI.HtmlControls.HtmlInputButton btnPostBack;

private void Page_Load(object sender, System.EventArgs e)
{

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.btnPostBack.ServerClick += new
System.EventHandler(this.btnPostBack_ServerClick);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void btnPostBack_ServerClick(object sender, System.EventArgs e)
{
Response.Write("<br>Page is posted back at: " +
DateTime.Now.ToLongTimeString());
}
}
----------------------------------------------------

Please check out my suggestions. If you have any questions on them or need
any further assistance, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

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

Nov 18 '05 #3
A slightly different way to handle this, using the framework instead of
ignoring it:

It is possible to give a CustomValidator javascript code, to execute on the
client.
There you can do your validation (but don't forget to validate on the server
as well, for the case where javascript has been switched off).
Look it up in the ducumentation, I have never used it (yet) so I can't help
you further with this.

Hans Kesting
"Gary Vidal" <ga**@usballoon.net> wrote in message
news:u9****************@tk2msftngp13.phx.gbl...
I have a client side Javascript which checks an OrderQuantityField against a
hidden Textbox of the Minimum Order Quantity. I dont want to do validation
on a postback. I would like to be able to have FormValidate Javascript
function postback to my addToCart Method in my code behind

function ValidateForm(myform) {
if (myform.OrderQuantity.value == 0) {
alert("Form is Invalid = 0 " + myform.OrderQuantity.value); return false
}
if (parseInt(myform.OrderQuantity.value,10) >=
parseInt(myform.MinQuantity_Hidden.value,10)) {
}
else { alert("Order Quantity must be greater than " +
myform.MinQuantity_Hidden);
myform.OrderQuantity.focus() return false
}
if (parseInt(myform.OrderQuantity.value,10) %
parseInt(myform.MinQuantity_Hidden.value,10))
{ alert("Order Quantity must be in multiples of " +
myform.MinQuantity_Hidden.value);

myform.OrderQuantity.focus()
return false
}
//Some code here to post back to the server method??

return true
}
Nov 18 '05 #4
It looks like you'd be happy with the Microsoft validator controls if they
only supported alert box messages and setting focus to the field with the
error. While that's not available with all that custom code you've been
writing, there is a commercial solution. My "Professional Validation And
More" (http://www.peterblum.com/vam/home.aspx) provides the ability to set
focus to the field with error, show an alert, change the color of the field
with the error or its label and even blink the error message. You will be
able to use it like Microsoft's validators, simply by dropping a validator
control onto the web form and set properties. There are 22 validators
included so many cases are available that would require custom coding in
Microsoft's system. I hope this helps.

--- Peter Blum
www.PeterBlum.com
Email: PL****@PeterBlum.com
Creator of "Professional Validation And More" at
http://www.peterblum.com/vam/home.aspx

"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:eP**************@TK2MSFTNGP10.phx.gbl...
A slightly different way to handle this, using the framework instead of
ignoring it:

It is possible to give a CustomValidator javascript code, to execute on the client.
There you can do your validation (but don't forget to validate on the server as well, for the case where javascript has been switched off).
Look it up in the ducumentation, I have never used it (yet) so I can't help you further with this.

Hans Kesting
"Gary Vidal" <ga**@usballoon.net> wrote in message
news:u9****************@tk2msftngp13.phx.gbl...
I have a client side Javascript which checks an OrderQuantityField against a hidden Textbox of the Minimum Order Quantity. I dont want to do validation on a postback. I would like to be able to have FormValidate Javascript
function postback to my addToCart Method in my code behind

function ValidateForm(myform) {
if (myform.OrderQuantity.value == 0) {
alert("Form is Invalid = 0 " + myform.OrderQuantity.value); return false }
if (parseInt(myform.OrderQuantity.value,10) >=
parseInt(myform.MinQuantity_Hidden.value,10)) {
}
else { alert("Order Quantity must be greater than " +
myform.MinQuantity_Hidden);
myform.OrderQuantity.focus() return false
}
if (parseInt(myform.OrderQuantity.value,10) %
parseInt(myform.MinQuantity_Hidden.value,10))
{ alert("Order Quantity must be in multiples of " +
myform.MinQuantity_Hidden.value);

myform.OrderQuantity.focus()
return false
}
//Some code here to post back to the server method??

return true
}

Nov 18 '05 #5
Hi Gary,

Have you had a chance to try out all the suggestions in the former replies
or have you got any further ideas on this issue?
If you have any questions or need any assitance, please feel free to post
here.
Regards,

Steven Cheng
Microsoft Online Support

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

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Ken Allen | last post: by
1 post views Thread by Jim Hammond | last post: by
2 posts views Thread by Robert Wagner | last post: by
3 posts views Thread by deride | last post: by
3 posts views Thread by =?Utf-8?B?R3JlZyBN?= | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.