Subject: Javascript DNN Control Validation
Prev Next
You are not authorized to post a reply.

AuthorMessages
goforebrokeUser is Offline
New Poster
New Poster
Posts:20

08/01/2010 11:02 PM  

I am trying to validate an asp textbox via javascript but I can't get the javascript code to validate the textbox control.

Textbox markup in the usercontrol:

Here is my code in the page load event that register the javascript code block

Dim cs As ClientScriptManager = Page.ClientScript
            If Not cs.IsClientScriptBlockRegistered(csType, csEmailCheck) Then
                Dim csText As New StringBuilder()
                csText.Append(Environment.NewLine + "[script removed]                 csText.Append(Environment.NewLine + "function CheckSubject(){")
                csText.Append(Environment.NewLine + "  if (document.getElementById(""<%=txtEmailSubject.ClientID%>"").value=="""")")
                csText.Append(Environment.NewLine + " {")
                csText.Append(Environment.NewLine + " var answer = confirm (""Do you want to send an email without a subject?"")")
                csText.Append(Environment.NewLine + " if (answer)")
                csText.Append(Environment.NewLine + " return true;")
                csText.Append(Environment.NewLine + " else")
                csText.Append(Environment.NewLine + " return false;")
                csText.Append(Environment.NewLine + "}")
                csText.Append(Environment.NewLine + "}")
                csText.Append(Environment.NewLine + "[script removed]")
                Page.ClientScript.RegisterClientScriptBlock(csType, csEmailCheck, csText.ToString())
            End If
            btnSendMail.Attributes.Add("onclick", "return CheckSubject()")
 

The javascript output is below

<script type="text/javascript">
function CheckSubject(){
  if (document.getElementById("<%=txtEmailSubject.ClientID%>").value=="")
 {
 var answer = confirm ("Do you want to send an email without a subject?")
 if (answer)
 return true;
 else
 return false;
}
}
script>

 

I know the problem is related to the way dnn names the controls when they are rendered to the page. Any suggestions?

 

Thank you

Mitchel SellersUser is Offline
Site Admin/Owner
Guru
Guru
Posts:6089

08/02/2010 1:37 AM  
I am going to give you two answers here.

1.) If you are just validating the field, I would NOT write your own JavaScript for this. ASP.NET provides controls for this (RequiredFieldValidator), use them, they are VERY helpful!
2.) YOur issue is that you are using .ascx markup on your actual code, you just need something like this.

csText.AppendFormat("document.getElementById('{0}').Text", me.txtEmailSubject.ClientId)

You can continue from there.

-Mitchel Sellers
Microsoft C# MVP, MCITP
Director of Development
IowaComputerGurus Inc.

View Mitchel Sellers's profile on LinkedIn

For shared hosting I recommend PowerDNN

This site is hosted on a Dedicated Server from PowerDNN.com

To get Guaranteed DNN Support check out our affordable DNN Technical Support Programs
goforebrokeUser is Offline
New Poster
New Poster
Posts:20

08/02/2010 11:10 AM  

Mitchel, thanks for the quick response. No I am not validating the field or I would use the delivered asp.net validation controls. I just want to inform the user that a field was left blank and if that is their actual intention. I will try your suggestion and post back tonight..

goforebrokeUser is Offline
New Poster
New Poster
Posts:20

08/03/2010 7:07 AM  

Mitchel, that worked perfect. I have one more question. How can check the contents of a dnn rich text editor?

I have tried csText.AppendFormat("if (document.getElementById(""{0}"").value=="""")", Me.rteEmailBody.ClientID)

but this generates a javascript error. Do you have any suggestions?

Thank you

 

goforebrokeUser is Offline
New Poster
New Poster
Posts:20

08/03/2010 11:23 AM  

I also tried csText.AppendFormat("if (document.getElementById(""{0}"").innerHTML=="""")", Me.rteEmailBody.ClientID) without luck

Mitchel SellersUser is Offline
Site Admin/Owner
Guru
Guru
Posts:6089

08/04/2010 1:23 AM  
That one I am not sure on, as I never interact with that control client side. The one thing you have to keep in mind is that a lot of times the editor has a default value of

or similar.

You could look at the rendered markup to see what the controls are that it renders to the page so you can see what JS would be needed to validate it.

-Mitchel Sellers
Microsoft C# MVP, MCITP
Director of Development
IowaComputerGurus Inc.

View Mitchel Sellers's profile on LinkedIn

For shared hosting I recommend PowerDNN

This site is hosted on a Dedicated Server from PowerDNN.com

To get Guaranteed DNN Support check out our affordable DNN Technical Support Programs
goforebrokeUser is Offline
New Poster
New Poster
Posts:20

08/06/2010 12:34 PM  

Thanks Mitchel.... I solved the problem with you recommendation of looking at the markup and a little assistance from google. In case anyone is interested here is the solution below.

 

Dim cs As ClientScriptManager = Page.ClientScript
            If Not cs.IsClientScriptBlockRegistered(csType, csEmailCheck) Then
                Dim csText As New StringBuilder()
                csText.Append(Environment.NewLine + "[script removed]                 csText.Append(Environment.NewLine + "function CheckEmail(){")
                csText.Append(Environment.NewLine)
                csText.Append(Environment.NewLine + "var answer")
                csText.Append(Environment.NewLine)
                If rteMail.EditMode = DotNetNuke.UI.WebControls.PropertyEditorMode.Edit Then
                    csText.AppendFormat("var oEditor = FCKeditorAPI.GetInstance(""{0}"");", Me.rteMail.ClientID + "edit")
                Else
                    csText.AppendFormat("var oEditor = FCKeditorAPI.GetInstance(""{0}"");", Me.rteMail.ClientID + "view")
                End If
                csText.Append(Environment.NewLine)
                csText.Append(Environment.NewLine + "var pageValue = oEditor.GetXHTML(true);")
                csText.Append(Environment.NewLine)
                csText.AppendFormat("if(document.getElementById(""{0}"").value=="""" && pageValue==""

&#160;

"")", Me.txtEmailSubject.ClientID)
                csText.Append(Environment.NewLine + " answer = confirm (""" + Localization.GetString("NoEmailSubjectAndBody", Me.LocalResourceFile) + """)")
                csText.Append(Environment.NewLine)
                csText.AppendFormat("else if(document.getElementById(""{0}"").value=="""")", Me.txtEmailSubject.ClientID)
                csText.Append(Environment.NewLine + " answer = confirm (""" + Localization.GetString("NoEmailSubject", Me.LocalResourceFile) + """)")
                csText.Append(Environment.NewLine + "else if(pageValue==""

&#160;

"")")
                csText.Append(Environment.NewLine + " answer = confirm (""" + Localization.GetString("NoEmailBody", Me.LocalResourceFile) + """)")
                csText.Append(Environment.NewLine)
                csText.Append(Environment.NewLine + " if (answer)")
                csText.Append(Environment.NewLine + " return true;")
                csText.Append(Environment.NewLine + " else")
                csText.Append(Environment.NewLine + " return false;")
                csText.Append(Environment.NewLine)
                csText.Append(Environment.NewLine + "}")
                csText.Append(Environment.NewLine + "[script removed]")
                Page.ClientScript.RegisterClientScriptBlock(csType, csEmailCheck, csText.ToString())
            End If
            btnSendMail.Attributes.Add("onclick", "return CheckEmail()")

goforebrokeUser is Offline
New Poster
New Poster
Posts:20

08/06/2010 12:44 PM  

Left a part out

csText.Append(Environment.NewLine + "var answer = new Boolean(""true"");")

You are not authorized to post a reply.
Forums >Development Discussion >DotNetNuke > Javascript DNN Control Validation



ActiveForums 3.7