显示qtip为包含重复特定的文本框 [英] Show qtip for specific textfields that contain duplicates

查看:110
本文介绍了显示qtip为包含重复特定的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了安全的问题和文本框供用户输入他们的答案的形式。我想prevent从相同的答案选定的每一个安全问题,键入用户。所以我想知道,如果我目前的做法是有道理的,服务器端验证,以prevent重复的答案提交。在用户类型有充分的答案是唯一的。

I have a form that has security questions and textfields for the user to type in their answer. I want to prevent the user from typing in the same answer to every security question selected. So I am wondering, if my current approach makes sense for server side validation to prevent duplicate answer submission. Every answer that the user types in has to be unique.

到目前为止,我有我的控制器,检查是否有答案的列表里面的DUP这个方法:

So far I have this method inside my controller that checks if there are dups in the list of answers:

public bool HasDuplicates(SecurityQuestions securityQuestions)
        {
            List<string> securityAnswers = new List<string>();
            securityAnswers.Add(securityQuestions.ChallengeA1);
            securityAnswers.Add(securityQuestions.ChallengeA2);
            securityAnswers.Add(securityQuestions.ChallengeA3);
            securityAnswers.Add(securityQuestions.ChallengeA4);
            securityAnswers.Add(securityQuestions.ChallengeA5);
            securityAnswers.Add(securityQuestions.ChallengeA6);

           bool hasDuplicates = securityAnswers.GroupBy(x => x).Where(g => g.Count() > 1).Any();

            if (hasDuplicates)
            {
                return true;
            }
            return false; 
        }

这对于是否有重复,但我想显示时,有重复,哪些字段包含重复的错误验证qtip检测工作正常。

This works fine for detecting if there are duplicates but I want to display an error validation qtip when there are duplicates and which fields contain the duplicates.

所以我想知道如果我做了验证,我上面只是告诉我,有重复,但不是哪些元素包含重复的方式。

So I'm wondering if I'm doing the validation the way I am above it just tells me that there are duplicates but not which elements contain the duplicates.

我如何知道哪些元素包含在上方和如何将它检查重复的?

How do I know which elements contain the duplicates in the check above and how to incorporate it?

此外,在这方面,我应该怎么会显示qtip验证错误,当我在做服务器端验证?

Also, in that regard, how should I be displaying the qtip validation errors when I'm doing server side validation?

我是否需要使用远程验证属性或我能做到一些其他的方式? ,这是什么最佳做法?

Do I need to use a remote validation attribute or can I do it some other way? What is best practice for this?

感谢。

推荐答案

您可以用得到重复值的集合

You can get a collection of the duplicate values using

var duplicates = securityAnswers.GroupBy(x => x).Where(g => g.Count() > 1).Select(g => g.Key);

然后你可以使用恢复到视图的JSON

which you could then return to the view as json using

return Json(duplicates);

和在你的Ajax调用

success: function(data) {
  if(data) {
    $.each(data, function() {
      // find the inputs where input.val() == $(this) and highlight
    }
  }
}

这篇关于显示qtip为包含重复特定的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆