Validating Captcha control

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
4/18/2010 2:58:47 PM
Gravatar
Total Posts 111
Matt Millican InternetMill

Validating Captcha control

Hi all,

Does anyone know how to validate whether the captcha control was filled out correctly?

I've tried using Joe's examples, but I think I am missing something small.

Thanks,

4/19/2010 12:29:00 PM
Gravatar
Total Posts 18439

Re: Validating Captcha control

Hi Matt,

Did you see my latest video http://www.mojoportal.com/dev-series-19-using-a-wysiwyg-editor-and-captcha.aspx ?

I cover using the Captcha there.

Hope it helps,

Joe

4/19/2010 12:32:08 PM
Gravatar
Total Posts 111
Matt Millican InternetMill

Re: Validating Captcha control

Hi Joe,

That's the first thing I looked at (I love that video series, by the way).

I'll take a look at it again - I'm probably just missing something simple.

Thanks,

Matt

4/19/2010 12:36:32 PM
Gravatar
Total Posts 18439

Re: Validating Captcha control

I was having the same problem I think in the video and my mistake was that  I was doing a redirect even when the page was invalid.

You have to call Page.Validate(); then check if(Page.IsValid)

If it isn't valid don't save the data and don't redirect to get out of postback.

Hope it helps,

Joe

4/19/2010 1:11:59 PM
Gravatar
Total Posts 111
Matt Millican InternetMill

Re: Validating Captcha control

Sounds good.  I will look into it tonight or tomorrow and let you know.

Thanks! :)

4/23/2010 9:15:40 AM
Gravatar
Total Posts 111
Matt Millican InternetMill

Re: Validating Captcha control

Hi Joe,

I went back through the video and double checked all my source code.  I still can't figure out why it's not validating the control.  

I'm going to try it from scratch, but just wanted to see if you had any additional thoughts.

Thanks,

Matt

4/24/2010 9:45:32 AM
Gravatar
Total Posts 18439

Re: Validating Captcha control

It is hard to diagnose without seeing the code.

Best,

Joe

4/29/2010 9:49:00 PM
Gravatar
Total Posts 111
Matt Millican InternetMill

Re: Validating Captcha control

Hi Joe,

Sorry it took so long to get this to you - I've been very busy this week.

If you could take a look through this, that would be great.

------------

protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.Load += new EventHandler(Page_Load);
        btnSend.Click += new EventHandler(btnSend_Click);
        Page.EnableViewState = true;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Hashtable moduleSettings = ModuleSettings.GetModuleSettings(ModuleId);
        if (WebUtils.ParseBoolFromHashtable(moduleSettings, "UseCaptcha", false) == true)
        {
            //Load captcha
            captcha.ProviderName = siteSettings.CaptchaProvider;
            captcha.Captcha.ControlID = "captcha" + ModuleId.ToInvariantString();
            captcha.RecaptchaPrivateKey = siteSettings.RecaptchaPrivateKey;
            captcha.RecaptchaPublicKey = siteSettings.RecaptchaPublicKey;

            captcha.Visible = true;
        }
        else
        {
            captcha.Visible = false;
            panelForm.Controls.Remove(captcha);
        }

        //If page is not post back, put URL into label to display and hidden form field
        if (!Page.IsPostBack)
       {
  string currentPage = SiteUtils.GetCurrentPageUrl();
  lblPage.Text = currentPage;
  hidPageUrl.Value = currentPage;
       }
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {

        Submit();

        //validate the page
        Page.Validate();
        if (!Page.IsValid) { return; }

        WebUtils.SetupRedirect(this, Request.RawUrl);
    }

    private void Submit()
    {
        string html = string.Empty;
        string cutUrl = string.Empty;
        string fromEmail = string.Empty;
        string yourName = string.Empty;
        string yourEmail = string.Empty;
        string friendEmail = string.Empty;
        string page = string.Empty;
        string subject = string.Empty;
        string message = string.Empty;
        string mailServer = string.Empty;

        subject = "Referring you to this web page";

        fromEmail = Settings["SharePageFromEmail"].ToString();

        yourName = Server.HtmlEncode(txtYourName.Text);
        yourEmail = Server.HtmlEncode(txtYourEmail.Text);
        friendEmail = Server.HtmlEncode(txtFriendEmail.Text);
        message = Server.HtmlEncode(txtMessage.Text);
        page = Server.HtmlEncode(hidPageUrl.Value);

        html = "";
        html += "<P><FONT FACE=\"Arial\" COLOR=\"Black\" SIZE=\"4\"><B>" + yourName + " referred this web page to you:</B></FONT></P>";
        html += "<P><FONT FACE=\"Arial\" COLOR=\"Black\" SIZE=\"2\"><A HREF=\"" + page + "\">" + page + "</A></FONT></P>";
        if (message.Length > 0)
            html += "<P><FONT FACE=\"Arial\" COLOR=\"Black\" SIZE=\"2\">" + message + "</FONT></P>";

        try
        {
            Email.SendEmail(SiteUtils.GetSmtpSettings(), fromEmail, yourEmail, friendEmail, "", "", subject, html, true, Email.PriorityNormal);

            lblMessage.Text = "Your message has been sent. Thank you!";
            lblMessage.Visible = true;

            panelForm.Visible = false;

            ClearForm();
        }
        catch (Exception ex)
        {
            log.Error(ex);

            lblMessage.Text = ex.InnerException.Message;
            lblMessage.Visible = true;
        }
    }

 

-------

Thanks,
Matt

4/30/2010 7:23:57 AM
Gravatar
Total Posts 18439

Re: Validating Captcha control

Hi Matt,

You should not call the Submit() method until after you call Page.Validate() and then only after checking for Page.IsValid

Also instead of removing the captcha if it is disabled just set its .Enabled property to false

Also if you are inheriting from SiteModule then you don't need this:

Hashtable moduleSettings = ModuleSettings.GetModuleSettings(ModuleId);

there is an intrinsic Settings object so you can just do

bool useCaptcha = WebUtils.ParseBoolFromHashtable(Settings, "UseCaptcha", false);

if(useCaptcha)...

Hope it helps,

Joe

4/30/2010 6:43:31 PM
Gravatar
Total Posts 111
Matt Millican InternetMill

Re: Validating Captcha control

Hi Joe,

I made the change to where submit() is called, and still no luck.

For the captcha, it's actually not using the SiteSettings.  I have another setting in ModuleSettings that makes it optional for the control to use the Captcha.

Sorry for being a pain, but do you have any other ideas as to what it might be?

Thanks,
Matt

5/1/2010 12:00:34 PM
Gravatar
Total Posts 18439

Re: Validating Captcha control

Have stepped through your code in the debugger to see what it is doing? You should set a break point and step through the code and make sure it is doing what you expect.

Hope it helps,

Joe

You must sign in to post in the forums. This thread is closed to new posts.