JQuery form submit, even validation returns false
My code below still represents the form in a special character in the name field. The validation works, but if I repeat it again, it crashes and submits the form with special characters.
What could be causing this?
$("#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
source to share
2 answers
In which browser can you reproduce this?
I did it on Firefox 3.5.2, but was unable to reproduce it on IE 6.0. After researching a few more, I noticed that on this line ...
if (noSpecialChars.test($("#name").val())) {
... the call to.test () returned true
and then false
in an interleaved pattern (Firefox only) suggesting some problem with RegExp
. So I tried to replace the implicit RegExp creation like this:
//var noSpecialChars = /[^a-zA-Z0-9]/g;
var noSpecialChars = new RegExp("[^a-zA-Z0-9]");
And that fixed the problem for me.
+3
source to share