Javascript validation

P

Peter Afonin

Hello,

I'm not an expert in Javascript, so I'm seeking an advice.

As I mentioned in my previous post, I use Javascript to check whether at
least one checkbox in the datagrid has been checked. I have to use
Stringbuilder to do this:

Dim iCount As Int32
Dim sClientSideValidate As New StringBuilder
iCount = dgReport.Items.Count
For i As Int32 = 2 To iCount + 1
If Not i = iCount + 1 Then
sClientSideValidate.Append("dgReport__ctl" & i & "_chkConf.checked ||")
Else
sClientSideValidate.Append("dgReport__ctl" & i & "_chkConf.checked")
End If
Next
btnSubmit.Attributes.Add("onClick", "if (!(" & sClientSideValidate.ToString
& ")){ return confirm('You have not selected any DRs. Are you sure you want
to submit change?');}")
'RELEASE RESOURCES
sClientSideValidate = Nothing

But now I my validation controls don't work, and I need to validate two more
controls. I need to make sure that a texbox txtReason has a value, and that
the ddlSystem.SelectedItem.Value <> 0.

I could just write a Javascript function to do all this without any
Stringbuilder and ASP.NET code, except checking datagrid. To do this I have
to use Stringbuilder. And I cannot figure out how to put all this validation
in a single string and make it work with Stringbuilder.

I would appreciate your suggestions very much.

Thank you,
 
A

Alexey Smirnov

Hello,

I'm not an expert in Javascript, so I'm seeking an advice.

As I mentioned in my previous post, I use Javascript to check whether at
least one checkbox in the datagrid has been checked. I have to use
Stringbuilder to do this:

Dim iCount As Int32
Dim sClientSideValidate As New StringBuilder
iCount = dgReport.Items.Count
For i As Int32 = 2 To iCount + 1
If Not i = iCount + 1 Then
sClientSideValidate.Append("dgReport__ctl" & i & "_chkConf.checked ||")
Else
sClientSideValidate.Append("dgReport__ctl" & i & "_chkConf.checked")
End If
Next
btnSubmit.Attributes.Add("onClick", "if (!(" & sClientSideValidate.ToString
& ")){ return confirm('You have not selected any DRs. Are you sure you want
to submit change?');}")
'RELEASE RESOURCES
sClientSideValidate = Nothing

But now I my validation controls don't work, and I need to validate two more
controls. I need to make sure that a texbox txtReason has a value, and that
the ddlSystem.SelectedItem.Value <> 0.

I could just write a Javascript function to do all this without any
Stringbuilder and ASP.NET code, except checking datagrid. To do this I have
to use Stringbuilder. And I cannot figure out how to put all this validation
in a single string and make it work with Stringbuilder.

I would appreciate your suggestions very much.

Thank you,

I think you need to add "return false;" to onClick event
 
A

Alexey Smirnov

Hello,

I'm not an expert in Javascript, so I'm seeking an advice.

As I mentioned in my previous post, I use Javascript to check whether at
least one checkbox in the datagrid has been checked. I have to use
Stringbuilder to do this:

Dim iCount As Int32
Dim sClientSideValidate As New StringBuilder
iCount = dgReport.Items.Count
For i As Int32 = 2 To iCount + 1
If Not i = iCount + 1 Then
sClientSideValidate.Append("dgReport__ctl" & i & "_chkConf.checked ||")
Else
sClientSideValidate.Append("dgReport__ctl" & i & "_chkConf.checked")
End If
Next
btnSubmit.Attributes.Add("onClick", "if (!(" & sClientSideValidate.ToString
& ")){ return confirm('You have not selected any DRs. Are you sure you want
to submit change?');}")
'RELEASE RESOURCES
sClientSideValidate = Nothing

But now I my validation controls don't work, and I need to validate two more
controls. I need to make sure that a texbox txtReason has a value, and that
the ddlSystem.SelectedItem.Value <> 0.

I could just write a Javascript function to do all this without any
Stringbuilder and ASP.NET code, except checking datagrid. To do this I have
to use Stringbuilder. And I cannot figure out how to put all this validation
in a single string and make it work with Stringbuilder.

I would appreciate your suggestions very much.

Thank you,

I think you need to add "return false;" to onClick event
 
P

Peter Afonin

Thank you, Alexey. For this particular statement I don't need return
false, I have no problem with this statement. I cannot figure out how
to combine this statement with validation of two other controls.

Peter
 
J

Jeremy Chapman

I wouldn't put your validation in the onClick javascript event of your
button. I would drop a CustomValidator control on your page, and populate
the ClientValidationFunction property with your javascript, as well as write
server side code in the ServerValidate event. Doing it this way is more
consistant with how all the other validation works.

Additionally, you don't need to create dynamic javascript using a string
builder. You can write javascript that parses the DOM, iterating through
all the rows of your html table until it finds a checkbox that is checked.
I think it's more elegant than generating a javascript method that checks
each control specifically via a really long piece of OR logic.

psuedo code:
for all rows in table
{
if checkbox in cell x of row is checked
{
return true
}
}
return false
 
P

Peter Afonin

Thank you very much, Jeremy, this makes sense. I've also just found
out that all validation controls would work if I use "confirm" instead
of "return confirm".

Peter
 

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

Top