使用jquery验证在错误消息中显示输入值 [英] show input value in error message using jquery validation

查看:76
本文介绍了使用jquery验证在错误消息中显示输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在文本字段中包含用户输入的值。我正在使用{0}键,但它返回true和false。 小提琴

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"


            }


        }
    })


推荐答案

您可以将值赋给this.value然后而不是字符串消息使用返回字符串的函数。在该函数内你可以访问this.value

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

看看这个 jsFiddle

$.validator.addMethod("specialChar", function(value, element){ 
  this.val = value;
  return false;
}, function() {
  return 'not allowed ' + this.val;
});

这篇关于使用jquery验证在错误消息中显示输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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