Reputation: 187
I'm having the following validation script in jquery: http://jsfiddle.net/0ybrx00q/
$("form").submit(function (event) {
if ($.isNumeric($("input[name=commodity]").val()) === false) {
$(".error_show").text("kosten müssen Zahl sein!").show();
event.preventDefault();
} else if ($("input[name=plz]").val().length != 5) {
$(".error_show").text("PLZ muss aus 5 Zeichen bestehen!").show();
event.preventDefault();
} else if ($.isNumeric($("input[name=leistung]").val()) === false) {
$(".error_show").text("Leistung muss Zahl sein!").show();
event.preventDefault();
} else if ($("input[name=nb]").val() === "") {
$(".error_show").text("Kein angebot!").show();
event.preventDefault();
} else {
$(".error_show").text("Validated...").show();
return;
}
});
In the external file I have saved the script in,there is another function that does work.However,I cannot use the above script to validate my form because even if I leave everything empty,you can press the submit button.The event.preventDefault() does not work there and there is also no "Validated..." message(so the script is just ignored).
Is there anything I'm missing?I can't seem to find any problem there since it is exactly the same code .
Upvotes: 0
Views: 53
Reputation: 26143
Wrap it in a document.ready handler - jsfiddle does this for you automatically...
$(function() {
$("form").submit(function (event) {
if ($.isNumeric($("input[name=commodity]").val()) === false) {
$(".error_show").text("kosten müssen Zahl sein!").show();
event.preventDefault();
} else if ($("input[name=plz]").val().length != 5) {
$(".error_show").text("PLZ muss aus 5 Zeichen bestehen!").show();
event.preventDefault();
} else if ($.isNumeric($("input[name=leistung]").val()) === false) {
$(".error_show").text("Leistung muss Zahl sein!").show();
event.preventDefault();
} else if ($("input[name=nb]").val() === "") {
$(".error_show").text("Kein angebot!").show();
event.preventDefault();
} else {
$(".error_show").text("Validated...").show();
return;
}
});
});
Upvotes: 3