0

我对 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
            }
        }
    });
});
4

1 回答 1

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
ZjA0NWJiMTFjNTAzZGEzZjViZmE2M2QzNThjNDk4ODlkZDViYTg0YTk1ZTc1YmU3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ZkOWRjMmNjNGM1NWRjMmYyNDEifX0=

并且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"}}
于 2013-11-15T19:19:40.623 回答