2

I just want to include value entered by the user in text field. I am using "{0}" key but it is returning true and false. fiddle

 $.validator.addMethod("specialChar", function(value, element){     
    var patt = new RegExp(/[^A-z]/g);
  return !patt.test(value)
 },"{0} is not valid");

    $('#myform').validate({
        onfocusout:function(element){           
            this.element(element)
        },
        rules:{
            username:{
                required:true,
                specialChar:true
            }
        },

        messages:{
            "username": {
                required: "Please enter user name"


            }


        }
    })
1
  • 2
    In patt.test(value) - test method will return boolean only. So you are appending boolean to your return message. Commented Apr 7, 2015 at 12:15

1 Answer 1

5

You can assign the value to this.value and then instead of string message use function that returns string. Inside that function you have access to this.value

Have a look at this jsFiddle

$.validator.addMethod("specialChar", function(value, element){ 
  this.val = value;
  return false;
}, function() {
  return 'not allowed ' + this.val;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.