About .NET, ASP.NET, MVC, C#, WPF, WCF and everything related to .NET and more.

Captcha Mvc [Demo (Plain captcha)]

The captcha render with Html.Captcha("Try another", "Enter the text you see above:", 5, "Is required field", true)




Enter the text you see above:


Comments

Hi,
I really liked this post and I implemented the solution for our site. I've a query relating to showing client side validation. Is there a way i can pass my model property which contains required attribute and attribute to check if captcha is valid or not.

Currently I check if captcha is valid as per your sample code in controller as following. I would like to show error message below the auto generated input field. With below code the textbox background color is changed to red but the message is not show.

'sample code
If Not Me.IsCaptchaValid("Captcha is not valid") Then
ModelState.AddModelError("CaptchaInputText", "Captcha is not valid")
Return View()
End If

Hi Vaibhav,
Try to create a captcha using the code:

var captcha = Html.Captcha("Refresh", "Input", 5, "Is required field.", false);
@captcha
@Html.ValidationMessage(captcha.BuildInfo.InputElementId)
or for the math captcha:
var mathCaptcha = Html.MathCaptcha("Refresh", "Input", "Is required field.", false);
@mathCaptcha
@Html.ValidationMessage(mathCaptcha.BuildInfo.InputElementId)
then in controller you can write this code:
this.IsCaptchaValid("Captcha is not valid")
and Captcha is not valid message will be automatically showed in the auto generated input field.

hi frnd
plz send complete code :rairahul9934@gmail.com this mail id

Hi Vyacheslav,

Thanks a lot for your solution. It worked like a charm..!! :)

Hi Vyacheslav,

While testing the captcha I found out that the captcha checks the text case insensitive. Is there any attibute that i can set to check the captcha should be checked for case.

As per my understanding the CAPTCHA image is always generated in uppercase.?

Thanks for your help.

Hi Vaibhav,
As per my understanding the CAPTCHA image is always generated in uppercase.?
Yes, these "QWERTYUIPASDFGHJKLZCVBNM" symbols are used by default. You can change it using this code:

protected void Application_Start()
{
    var captchaManager = (DefaultCaptchaManager) CaptchaUtils.CaptchaManager;
    captchaManager.CharactersFactory = () => "my characters";
Is there any attibute that i can set to check the captcha should be checked for case.
Yes, you can change this behavior using this code:
captchaManager.PlainCaptchaPairFactory = length =>
{
string randomText = RandomText.Generate(captchaManager.CharactersFactory(), length);
bool ignoreCase = false;//This parameter is responsible for ignoring case.
return new KeyValuePair<string, ICaptchaValue>(Guid.NewGuid().ToString("N"),
    new StringCaptchaValue(randomText, randomText, ignoreCase));
};

Thanks it helped. Also noticed that after this change the captcha is generated in mixed mode i.e with letter and number and symbols also with upper and lower case mix.

Thanks
Vaibhav

This seems like a good solution BUT would be nice if you actually had the complete code and how this fits within a form post (maybe with one other non-captcha input). I don't know if I am missing something or need to browse through the dll but some things aren't adding up. But I am sure once it becomes clear I will go AHA! For now I think the solution could use a little more clarity. ( ;

Is there a way to catch this on the client side before doing a post?

Hi, How can i check validation for Captcha in Javascript.

Hi.
Is it possible to only show numbers in captcha?

I am wondering how can I set url link for Refresh picture redirection. Thanks.

Could it be that there is a bug with the .IsCaptchaValid() method? It always return false, whether I type the correct captcha or the incorrect captcha. That is really annoying!!!

Hi,
Can you provide more information about the problem, or could you please give a demo project to demonstrate this issue?
When did it happened, during debugging or in production?
Which version of CaptachMvc are you using?
Which version of MVC are you using?
Did you have any exception when the captcha was created?

Hi, I hope you are well.

I started to use this library to replace google's reCaptcha code because of organizational requirements.
Great functionality and code. You did a nice work! congratulations.

I noticed as Superbyte that the method IsCaptchaValid() has a bug and indeed shows that the captcha is not valid when it is.

I'm using your MVC4 example project.
This happened in development machine during debugging.
I'm using the 2.5.0.0 version and i have no exceptions when captcha was created.

The strange thing is that i integrated this captcha code to another project and in development during debugging the method IsCaptchaValid is not showing the bug...

Could you provide a solucion or a hint about what's happening?

Thanks.

I forgot to mention that in the second project I'm using MVC4, no exceptions or errors are showed during captcha creation and the library version is 2.5.0.0 too. Thanks!

Hi,

I found the fix to this behaviour.

Let's say that you have this controller method:

[HttpPost, CaptchaVerify("Captcha is not valid")]
public ActionResult PartialCaptchaView(string empty)
{
//...your code...
if (this.IsCaptchaValid("Captcha is not valid")){
//more code..
}
//more code...
}

to make method "IsCatpchaValid" work you just have to remove "CaptchaVerify" inside the brackets.
If you want to use "ModelState.IsValid" property leave "CaptchaVerify" inside the brackets.

I hope this helps and sorry for the previous reply.

Hi,

I am getting the error below when I see my form page with Developer Tools (F12) in Chrome:

"Uncaught ReferenceError: $ is not defined"

In page source code:

<script>
$(function () {$('#1be752a6bbab42f687fda6c4f4f64dc7').show();}); <<--- ERROR IN THIS LINE !!!
function 53ccf101899b4ae6959118467563d157() { $('#1be752a6bbab42f687fda6c4f4f64dc7').hide(); $.post("/Maclau.Web/DefaultCaptcha/Refresh", { t: $('#CaptchaDeText').val() }, function(){$('#1be752a6bbab42f687fda6c4f4f64dc7').show();}); return false; }</script>

Do you know how to remove this error ?

Thanks,
Marcelo.

Hi Marcelo,

Maybe you haven't added JQuery script, try to declare jQuery script in your layout page and put it in the head tag or try to split the captcha markup and script generation.

I would like to have image instead of refresh as text. How to do that?

Hi Joyce,

If you need a flexible set-up, I advise you to use the partial-view, with their help, you can fully control the markup for captcha.
Examples of forms:
MvcCaptcha.Examples.Mvc4\GettingStarted\Views\Home\MathPartialCaptchaView.cshtml
MvcCaptcha.Examples.Mvc4\GettingStarted\Views\Home\PartialCaptchaView.cshtml

Captcha image is not rendered. I am using CaptchaMvc.Mvc4

How I add class for this input

Thanks

The captcha render with Html.Captcha("Try another", "Enter the text you see above:", 5, "Is required field", true)

The "Try another" option doesn't appear. How can i put it?

Hi,

Is there any way to do the "Try another" in ajax mode ? In my MVC project, every time I click the link, the page is posted and it rolls the scrollbar. I´d like to change de code without rolling the page....

Another doubt is if I can use an image in this link.

Thanks.

Hello, how can i implement this captcha without page being re positioned on refresh?i.e every time i tried to refresh captcha the page gets pulled(scrolled) down..so how can i set this issue?and also how can i customize the refresh button look?please help..

hey,,i found the example http://captchamvc.codeplex.com/releases/view/114341 wich doesn' t pulls down the page on refresh captcha(uses ajax)....

captcha showing as black in safari and iphone can you help me fix this

I have the same problem. captcha is a black image on safari and Iphone. thanks

Hi,
I need to add audio to this captcha, can u guide me?

Hi,
Can you please explain how to validate Captcha-MVC using ajax post method without using any form.

Hi,
I implemented captcha in my contact form.
View:

var captcha=Html.Captcha("Refresh", "Input", 5, "Is required field.", false);
@captcha

Here,it shows error message as " captcha" does not exist in current context.
What I did wrong in this? please advise.

Hi,

I'm validating captcha in JsonResult Action. But if i put wrong captcha and my JsonResult method returns false. But captcha is not reloading. I want to reload captcha for this.

Can I make the captcha check optional. For example it should not validate when user is logged in and authenticated? only show for users who are not authenticated

Leave a Reply
*bold*
_italics_
+underline+
* Bullet List
** Bullet List 2
# Number List
## Number List 2
{"Do not apply formatting"}
{code:language} code here {code:language}.
Supports: aspx c#, c#, c++, html, sql, xml
[url:http://www.example.com]