473,320 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 2136
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ken Allen | last post by:
I am relatively new to .Net and C#, but I hav ebeen programing in other languages and done some COM work for a number of years. I am attempting to understand how to map an older program...
1
by: Jim Hammond | last post by:
I can get data from a client-side assembly to the server in two manual steps, but I need to be able to do it in one step. Step 1: The user presses the manually coded "Step 1" button, which calls...
2
by: Robert Wagner | last post by:
I've created a page using VS.NET and page validator controls. The client side validation works fine on IE, but does not even activate under "alternate" browsers like Mozilla, Opera, etc. Why is...
4
by: | last post by:
Hello Guys, I am using the validation controls to validate my data. But the problem is "The page is still being posted to server". I want to get rid of the round trips to server. Are there...
2
by: Dnna | last post by:
I have a table which is bound to an Internet Explorer XML data island. I'm using ASP.NET's client-side validators for an input field in the table. The problem is that if the input fields are in...
3
by: Arun K | last post by:
Hi, I am creating a simple .aspx page to add some fields with validation. I have used different .NET validations like REquiredFieldValidator, RegularExpressionValidator and showed the summary...
3
by: deride | last post by:
For client side validation, the following code is generated by the framework <form name="Form1" method="post" onsubmit=" if (!ValidatorOnSubmit()) return false; MyFunction..."> My Problem is...
1
by: karen987 | last post by:
I have a comment form, on a news website, ASP page, which users fill in and it adds comments to a news article. The reader clicks on a headline and the comments open up in a new window. It already...
3
by: =?Utf-8?B?R3JlZyBN?= | last post by:
Hello, I'm running an asp.net, intranet web application using .net framework 1.1 on IIS5.1 / 6.0. Through the web application, I would like to press a button on the web page, have another window...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.