Validation on dynamic fields

G

gary.smith

Hello,

I am building an table out of dynamic data for the purposes of editing
rows. The scope of the project didn't allow for an elegant way of
doing this with forms.

Basically I add a control a cell to a row to the table. That control
has a validation associated with it. The problem is on the next
postback I need to do a bunch of odd stuff to it so my first thought
was to use some type of client validation. In it's simplist form, I
basically want to validate that the field is of type currency. This is
simple enough in JS or in RegExp.

Here is a sample of the logic:

oCell = New System.Web.UI.WebControls.TableCell
oTextbox = New System.Web.UI.WebControls.TextBox
oTextbox.ID = "LastMaintenanceCost_" & sUniqueRowID
oTextbox.Text =
LastMaintenanceCost.ToString("$#,##0.00;($#,##0.00);$0.00")
oTextbox.Width =
System.Web.UI.WebControls.Unit.Point(50)
oCell.Controls.Add(oTextbox)

oRequiredValidator = New
System.Web.UI.WebControls.RequiredFieldValidator
oRequiredValidator.ID = "LastMaintenanceCostRequired_"
& sUniqueRowID
oRequiredValidator.ControlToValidate =
"LastMaintenanceCost_" & sUniqueRowID
oRequiredValidator.ErrorMessage = "<br>Required"
oRequiredValidator.Display = ValidatorDisplay.Dynamic
oRequiredValidator.EnableClientScript = True
oCell.Controls.Add(oRequiredValidator)

oRegExpValidator = New
System.Web.UI.WebControls.RegularExpressionValidator
oRegExpValidator.ID = "LastMaintenanceCostRegExp_" &
sUniqueRowID
oRegExpValidator.ControlToValidate =
"LastMaintenanceCost_" & sUniqueRowID
oRegExpValidator.ErrorMessage = "<br>Currency values
only"
oRegExpValidator.Display = ValidatorDisplay.Dynamic
oRegExpValidator.EnableClientScript = True
oRegExpValidator.ValidationExpression =
"^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$"

oCell.Controls.Add(oRegExpValidator)
oRow.Cells.Add(oCell)

So my question is what do I need to do to make this logic execute on
the client side? I have enabled client scripting but that doesn't seem
to do anything.

Also, is the validation code IE specific or does it work on browsers
such as Mozilla or FireFox?
 
B

Brock Allen

The server side validation controls always run server side and are supported
client side in IE only, unfortunately.
 
E

Elazar

Hello,
Try looking into the RegisterClientScriptBlock method
(http://msdn.microsoft.com/library/d...uipageclassregisterclientscriptblocktopic.asp)
and the RegisterOnSubmitStatement
method(http://msdn.microsoft.com/library/d...uipageclassregisterclientscriptblocktopic.asp)

Hope this helps!

--
Elazar


Brock Allen said:
The server side validation controls always run server side and are supported
client side in IE only, unfortunately.




Hello,

I am building an table out of dynamic data for the purposes of editing
rows. The scope of the project didn't allow for an elegant way of
doing this with forms.

Basically I add a control a cell to a row to the table. That control
has a validation associated with it. The problem is on the next
postback I need to do a bunch of odd stuff to it so my first thought
was to use some type of client validation. In it's simplist form, I
basically want to validate that the field is of type currency. This
is simple enough in JS or in RegExp.

Here is a sample of the logic:

oCell = New System.Web.UI.WebControls.TableCell
oTextbox = New System.Web.UI.WebControls.TextBox
oTextbox.ID = "LastMaintenanceCost_" & sUniqueRowID
oTextbox.Text =
LastMaintenanceCost.ToString("$#,##0.00;($#,##0.00);$0.00")
oTextbox.Width =
System.Web.UI.WebControls.Unit.Point(50)
oCell.Controls.Add(oTextbox)
oRequiredValidator = New
System.Web.UI.WebControls.RequiredFieldValidator
oRequiredValidator.ID = "LastMaintenanceCostRequired_"
& sUniqueRowID
oRequiredValidator.ControlToValidate =
"LastMaintenanceCost_" & sUniqueRowID
oRequiredValidator.ErrorMessage = "<br>Required"
oRequiredValidator.Display = ValidatorDisplay.Dynamic
oRequiredValidator.EnableClientScript = True
oCell.Controls.Add(oRequiredValidator)
oRegExpValidator = New
System.Web.UI.WebControls.RegularExpressionValidator
oRegExpValidator.ID = "LastMaintenanceCostRegExp_" &
sUniqueRowID
oRegExpValidator.ControlToValidate =
"LastMaintenanceCost_" & sUniqueRowID
oRegExpValidator.ErrorMessage = "<br>Currency values
only"
oRegExpValidator.Display = ValidatorDisplay.Dynamic
oRegExpValidator.EnableClientScript = True
oRegExpValidator.ValidationExpression =
"^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$"
oCell.Controls.Add(oRegExpValidator)
oRow.Cells.Add(oCell)
So my question is what do I need to do to make this logic execute on
the client side? I have enabled client scripting but that doesn't
seem to do anything.

Also, is the validation code IE specific or does it work on browsers
such as Mozilla or FireFox?
 

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,774
Messages
2,569,599
Members
45,177
Latest member
OrderGlucea
Top