IM在我的網站:如何使用這種形式的驗證腳本添加選擇框表單驗證
$(document).ready(function(){
// Place ID's of all required fields here.
required = ["name", "email", "country", "message",];
// If using an ID other than #email or #error then replace it here
email = $("#email");
errornotice = $("#error");
// The text to show up within a field when it is incorrect
emptyerror = "Please enter a value in field";
emailerror = "Please enter a valid e-mail.";
$("#theform").submit(function(){
//Validate required fields
for (i=0;i<required.length;i++) {
var input = $('#'+required[i]);
if ((input.val() == "") || (input.val() == emptyerror)) {
input.addClass("needsfilled");
input.val(emptyerror);
errornotice.fadeIn(750);
} else {
input.removeClass("needsfilled");
}
}
// Validate the e-mail.
if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
email.addClass("needsfilled");
email.val(emailerror);
}
//if any inputs on the page have the class 'needsfilled' the form will not submit
if ($(":input").hasClass("needsfilled")) {
return false;
} else {
errornotice.hide();
return true;
}
});
// Clears any fields in the form when the user clicks on them
$(":input").focus(function(){
if ($(this).hasClass("needsfilled")) {
$(this).val("");
$(this).removeClass("needsfilled");
}
});
});
的問題是,我沒有代碼的選擇框
任何人都可以給我的代碼驗證選擇框? 我的選擇框中的名稱和ID是「國」 並且如果選擇停留在「國家」我希望有一個錯誤通知
這個沒有工作unfortuantely – user2232538
其實我修改了它和它的工作:VAR國家= $( '#國家選項:選擇')。VAL(); \t \t \t如果(國家== 「」) \t \t \t { (「#msg」)。html(「請選擇國家」); \t \t \t return false; \t \t \t} – user2232538