私はJavaScriptとjqueryを学んでいて、特殊なスタイリングがあれば、私のフォームのいくつかの入力が満たされているかどうかを確認するために次のコードを書いています。シンプルなライブjqueryフォームバリデーターのコード構造
コードは正常に動作しますが、これを書き込むにはどうすればよいでしょうか?
$(document).ready(function() {
$("#id_email").change(function() {
var emailField = document.getElementById("id_email").value;
if (emailField.indexOf("@") >= 0 && emailField.indexOf(".") >= 0) {
$(this).attr(/* styling my field */);
} else {
$(this).attr(/* styling my field */);
}
});
$("#id_last_name").change(function() {
var nameField = document.getElementById("id_last_name").value;
if (nameField.length > 0) {
$(this).attr(/* styling if true */);
} else {
$(this).attr(/* styling if false */);
}
});
$("#id_phone").change(function() {
var phoneField = document.getElementById("id_phone").value;
if (phoneField.length > 0) {
$(this).attr(/* styling if true */);
} else {
$(this).attr(/* styling if false */);
}
});
$("#id_company").change(function() {
var companyField = document.getElementById("id_company").value;
if (companyField.length > 0) {
$(this).attr(/* styling if true */);
} else {
$(this).attr(/* styling if false */);
}
});
$("#id_activity").change(function() {
var activityField = document.getElementById("id_activity").value;
if (activityField !== "---------") {
$(this).attr(/* styling if true */);
} else {
$(this).attr(/* styling if false */);
}
});
});