jquery livevalidation

时间:2012-11-23 10:16:26

标签: jquery livevalidation

我正在使用名为livevalidation(http://livevalidation.com/)的脚本,这是我对此特定ID的编码:

var First_Name = new LiveValidation( 'First_Name');
First_Name.add(Validate.Presence ); --this means it must have a value
First_Name.add(Validate.Format, { pattern: /^[a-z]+$/i} ); --this means it must be letters only

如果它没有一个值无效,那么现在不要提交我想要重复的字符使得变得无效以及如果有人在名称中点击相同的字母时间然后它将变为无效

Format: function(value, paramsObj){
  var value = String(value);
  var paramsObj = paramsObj || {};
  var message = paramsObj.failureMessage || "Not valid!";
  var pattern = paramsObj.pattern || /./;
  var negate = paramsObj.negate || false;
  if(!negate && !pattern.test(value)) Validate.fail(message); // normal
  if(negate && pattern.test(value)) Validate.fail(message); // negated
  return true;
},

您的编码有效,但我如何将编码格式化为此格式我希望它在无效时输出消息

1 个答案:

答案 0 :(得分:2)

您可以使用自定义验证:

//Define function to check input values
var noRepeatativeChars = function(val) {
   val = val || ""; //Handle null and undefined.
   var chars = val.split(""),  //Convert string to char array
       len = chars.length - 1, i = 0;

   for(; i < len; i++) {
      if(chars[i] === chars[i+1]) { //If current char === next char.
         return false;
      }
   }
   return true;
};

//Add custom validator.
First_Name.add(Validate.Custom, {against: noRepeatativeChars, failureMessage: "Go read the manual!"});