使用addMethod()的jquery验证器自定义规则

时间:2013-11-15 00:41:09

标签: jquery validation

我是jquery验证器的新手,我在使用addMethod解决如何使用自定义规则扩展它时遇到了一些麻烦。我需要从文本字段中获取base64编码的内容,修剪换行符并对其进行解码。这是我到目前为止所做的,但是很难排除故障,因为我无法让表单停止自动提交。谁能告诉我为什么这两种方法都不起作用?

$.validator.addMethod("trimLineFeeds", function(value, element) {
    return this.optional(element) || value.replace(/\\r\\n/g, "");
}, "Please check your input.");

$.validator.addMethod("b64Decode", function(value) {
    var e={},i,b=0,c,x,l=0,a,r='',w=String.fromCharCode,L=value.length;
    var A="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    for(i=0;i<64;i++){e[A.charAt(i)]=i;}
    for(x=0;x<L;x++){c=e[value.charAt(x)];b=(b<<6)+c;l+=6;
        while(l>=8){((a=(b>>>(l-=8))&0xff)||(x<(L-2)))&&(r+=w(a));}
    }
    return r;
}, "Please check your input.");

$(document).ready(function() {
        $("#myForm").validate({
        rules: {
            b64Field: {
                regex:      '^[a-zA-Z0-9=\\s]*$',
                maxlength:  8192,
                minlength:  512,
                required:   true,
                trimLineFeeds:  true,
                b64Decode:  true
            }
        }
    });
});

1 个答案:

答案 0 :(得分:0)

为了更好地解释我的目标,我需要获取一些phpseclib密钥信息,即json_encode(),base64_encode(),chunk_split(),通过电子邮件发送和反转浏览器中的整个过程,通过复制/粘贴到本地存储textarea表格。现在我设法使我自己的代码正常工作了,我希望有一些信息可以帮助其他人,我希望能够帮助其他人。

通过更多的阅读和修补,我在上面找到的Base64 / UTF-8编码/解码库的帮助下,设法得到了我在上面尝试做的事情: https://gist.github.com/Nijikokun/5192472

注意:将base64-utf8.module.js的本地副本保存到您的服务器,因为在GitHub中使用它像CDN似乎并不适用于所有情况。我的浏览器(Chrome v30.0.1)抱怨它是text / plain而不是text / javascript或application / javascript,如果运行,最终会出错Uncaught ReferenceError: base64 is not defined

以下是更新代码的完整副本。

$.validator.addMethod("trimAndDecode", function(value) {
    value = value.trim();
    value = value.replace(/[\r\n]*/g, "");
    value = base64.decode(value);
    return value;
}, "Please check your input.");

$(document).ready(function() {
        $("#myForm").validate({
        rules: {
            b64Field: {
                regex:      '^[a-zA-Z0-9=\\s]*$',
                maxlength:  8192,
                minlength:  512,
                required:   true,
                trimAndDecode:  true
            }
        }
    });
});

因此,当提交chunk_split块时,base64编码的内容如下:

eyJjbGllbnQiOnsicHVibGljS2V5IjoiLS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0t
LS1cclxuTUN3d0RRWUpLb1pJaHZjTkFRRUJCUUFER3dBd0dBSVJBSjZmK3cydzRU
TWxFN2dUeEt1K0FLTUNBd0VBQVE9PVxyXG4tLS0tLUVORCBQVUJMSUMgS0VZLS0t
LS0iLCJwcml2YXRlS2V5IjoiLS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0t
LVxyXG5NR0VDQVFBQ0VRQ2VuXC9zTnNPRXpKUk80RThTcnZnQ2pBZ01CQUFFQ0VB
V1UxMzZaMUxLNTh4TUw5UFB6cStFQ1xyXG5DUURMeE12b2grVGEyd0lKQU1kSTNw
NDBOWWZaQWdoMUp2VVJBS2hjQndJSWJmckJJUDRmTE1FQ0NFbWxGUjArXHJcbk5j
SWFcclxuLS0tLS1FTkQgUlNBIFBSSVZBVEUgS0VZLS0tLS0iLCJwYXNzUGhyYXNl
IjoiODAxYTY5MTZjMDFhMmMyYzQ0OTYyNGQwMzJlZGE2ODczNDFmNTk0NmUyMWYz
NmI0Y2NiNGMwYWI4YjRhMzg5MWY5NDc4YzllZjYwZDgzZTk5MzE3OTMwNjM0MTg4
OWRlM2IyYTRiMDlhYzVjMGY2YzMwNzRkZWVlYmM0N2FlMTZjM2ZhODFiOWYwNzk3
ODE2ZWQwNTkwZDU5NWM2N2MxYzllMmFkZTdjNWQ2ODBhZDIzYmI0MzY0NjY0NmM5
ZjA0NWJiMTFjNTAzZGEzZjViZmE2M2QzNThjNDk4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4NmQ1MThmNTg0NGIzZThjN2ZkOWRjMmNjNGM1NWRjMmYyNDEifX0=

并且base64解码为如下所示:

{"client":{"publicKey":"-----BEGIN PUBLIC KEY-----\r\nMCwwDQYJKoZIhvcNAQEBBQADGwAwGAIRAJ6f+w2w4TMlE7gTxKu+AKMCAwEAAQ==\r\n-----END PUBLIC KEY-----","privateKey":"-----BEGIN RSA PRIVATE KEY-----\r\nMGECAQACEQCen\/sNsOEzJRO4E8SrvgCjAgMBAAECEAWU136Z1LK58xML9PPzq+EC\r\nCQDLxMvoh+Ta2wIJAMdI3p40NYfZAgh1JvURAKhcBwIIbfrBIP4fLMECCEmlFR0+\r\nNcIa\r\n-----END RSA PRIVATE KEY-----","passPhrase":"801a6916c01a2c2c449624d032eda687341f5946e21f36b4ccb4c0ab8b4a3891f9478c9ef60d83e993179306341889de3b2a4b09ac5c0f6c3074deeebc47ae16c3fa81b9f0797816ed0590d595c67c1c9e2ade7c5d680ad23bb43646646c9f045bb11c503da3f5bfa63d358c49889dd5ba84a95e75be76c6c506e15b1c603ce7"},"server":{"publicKey":"-----BEGIN PUBLIC KEY-----\r\nMCwwDQYJKoZIhvcNAQEBBQADGwAwGAIRAMIWvggm9L7fa2q6OHoh64UCAwEAAQ==\r\n-----END PUBLIC KEY-----","privateKey":"","passPhrase":"152754c262ec656592a097b28515e09f05d87b602d893ac4654d3621e116784d96a28688171a2ceb1487f1e0f5380b1be9570c7f049e9ab1d8c904c0b81d96d2ac08af55cb8edf202cfd7a7e2139ceb957f82df75930b26c07f09d9bdc71670bdb17ae172141780c4c5e6fa6828886d518f5844b3e8c7fd9dc2cc4c55dc2f241"}}