0
我试图让Jquery使用正则表达式验证表单,但以非常规方式。表单ID是“sumform”,表单内的textarea被称为“summary”。我在包含正则表达式的表单外有另一个输入字段(id =“terms_out”)。我似乎无法让Jquery验证程序将“terms_out”的值识别为正则表达式。当用户输入任何内容时,我的表单都会被提交。使用包含RegEx和Jquery的输入字段来验证表单
我已经试过,但它不工作...
$(function() {
$.validator.addMethod("regex", function(value, element, regexpr) {
return regexpr.test(value);
}, "Sorry, you haven't used all the terms.");
$("#sumform").validate({
rules: {
summary: {
required: true,
regex: '#terms_out',
}
}
});
});
这工作完全正常......
$(function() {
$.validator.addMethod("regex", function(value, element, regexpr) {
return regexpr.test(value);
}, "Sorry, you haven't used all the terms.");
$("#myForm").validate({
rules: {
experience: {
required: true,
regex: /^(?=[\S\s]*\bcell|cells|cell\b)(?=[\S\s]*\bDNA|DNA|DNA\b)(?=[\S\s]*\bsense|senses|sense\b)(?=[\S\s]*\brespond|responds|respond\b)(?=[\S\s]*\benergy|energy|energy\b)(?=[\S\s]*\bgrow|grows|grow\b)(?=[\S\s]*\bdevelop|develops|develop\b)(?=[\S\s]*\breproduce|reproduces|reproduce\b)/
}
}
});
});
我在这所以任何帮助非常新会不胜感激。谢谢!
感谢您的帮助萨尔,但这似乎并不奏效。以下是我的摘要表单的链接http://wisscience7-lessons.weebly.com/summary-2-1.html - “terms_out”字段位于左上角。 – firenemus
嗨,我调试了你的代码,问题是你的validate函数在你向'terms_out'添加正则表达式之前被调用,请确保你的代码以正确的顺序运行 – Saar
再次感谢Saar的帮助。我很抱歉,但是如何在正则表达式可用之前知道validate函数正在被调用?我之前成功地使用了这个方法,除了它只是一个简单的数值,它将进入输入字段。再次谢谢你。 – firenemus