Search code examples
jquerylivevalidation

jquery livevalidation


I'm using a script called livevalidation (http://livevalidation.com/) and here is my coding for this specific 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

if it doesnt have a value invalid then wont submit now i want repetitive characters to make is become invalid as well like say if some one click the same letter times in the name then it will become invalid

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;
},

your coding works but how do i put the coding in this format i want it to output a message when its invalid


Solution

  • You can use custom validation:

    //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!"});