即使验证返回false,JQuery表单也会被提交

时间:2009-08-27 13:01:29

标签: jquery validation

我下面提到的代码仍然在名称字段中的特殊字符上提交表单。验证工作但是如果我反复提交它会中断并提交名称中带有特殊字符的表单。

这可能是什么原因?

$("#fee").submit(function(){
   trimmedValue = $.trim($("#name").val());
   $("#name").val(trimmedValue);
   typevalue = $("input:radio[@name='type']:radio:checked").val();
   if(typevalue=="FLAT" && !isFloat($("#amount").val())) {
   alert("Amount should be number with proper decimal formatting");
   $("#amount").val("0.0");
    return false;
}
var noSpecialChars = /[^a-zA-Z0-9]/g;
if (noSpecialChars.test($("#name").val())) {
    alert("Please enter valid fee Name. Do not enter special characters"  );
    return false;
}

if(typevalue=="PERCENTAGE" && !isFloat($("#percentage").val())) {
   alert("percentage should be number with proper decimal formatting");
   $("#percentage").val("0.0");
    return false;
}
if(typevalue=="FLAT" && $("#amount").val()=="0.0") {
    return confirm("Do you really want the amount to be 0.0 ?");
}
if(typevalue=="PERCENTAGE" && $("#percentage").val()=="0.0") {
    return confirm("Do you really want the percentage to be 0.0 ?");
}

});

2 个答案:

答案 0 :(得分:3)

您可以在哪个浏览器中重现这个?

我在Firefox 3.5.2上做过,但无法在IE 6.0上重现它。在探索了一些之后,我注意到了这一行......

if (noSpecialChars.test($("#name").val())) {

...对.test()的调用以交替模式(仅在Firefox中)返回true,然后是false,表明RegExp存在问题。所以我试着像这样替换RegExp的隐式创建:

    //var noSpecialChars = /[^a-zA-Z0-9]/g;        
    var noSpecialChars = new RegExp("[^a-zA-Z0-9]");

这解决了我的问题。

答案 1 :(得分:0)

在重复按下时,解释器似乎永远不会到达return false;,而是将表单提交给为表单指定的href

如果您的表单标记中有href,请尝试删除它并在调用返回之前插入该属性。

相关问题