我有一个表单验证将检查每个输入是否有效,但问题是当只有一个输入有效然后所有输入被认为是有效的。 我已经转移了返回的真实位置,但它没有改变任何东西。
非常感谢任何帮助。
<form action="me.php" method="post" id="myform" onsubmit="return(val());">
<input type="text" name="name" placeholder="Nama Anda *" class="i-form na"/>
<input type="text" name="email" placeholder="Alamat E-mail *" class="i-from em"/>
<input type="text" name="subject" placeholder="Judul Pesan *" class="i-form su"/>
<textarea class="i-form isi" placeholder="Isi Pesan *"></textarea>
<input type="submit" value="Send" class="btn"></input>
function val(){
var nama = $('.na').val();
var email = $('.em').val();
var subject = $('.su').val();
var isi = $('.isi').val();
if(nama === ''){
$('.na').attr('placeholder','Masukkan Nama Anda');
if(email === ''){
$('.em').attr('placeholder','Masukkan E-mail Anda');
if(subject === ''){
$('.su').attr('placeholder','Masukkan subject Pesan');
if(isi === ''){
$('.isi').attr('placeholder','Masukkan Isi Pesan');
return false;
}
}
}
} else {
return true;
}
}
答案 0 :(得分:3)
按照你的说法检查所有输入。
function val(){
var nama = $('.na').val();
var email = $('.em').val();
var subject = $('.su').val();
var isi = $('.isi').val();
var flag = true;
if(nama === ''){
$('.na').attr('placeholder','Masukkan Nama Anda');
flag = false;
}
if(email === ''){
$('.em').attr('placeholder','Masukkan E-mail Anda');
flag = false;
}
if(subject === ''){
$('.su').attr('placeholder','Masukkan subject Pesan');
flag = false;
}
if(isi === ''){
$('.isi').attr('placeholder','Masukkan Isi Pesan');
flag = false;
}
return flag;
}
答案 1 :(得分:0)
function val(){
var nama = $('.na').val();
var email = $('.em').val();
var subject = $('.su').val();
var isi = $('.isi').val();
if (nama && email && subject && isi) {
return true;
}
if(nama === ''){
$('.na').attr('placeholder','Masukkan Nama Anda');
if(email === ''){
$('.em').attr('placeholder','Masukkan E-mail Anda');
if(subject === ''){
$('.su').attr('placeholder','Masukkan subject Pesan');
if(isi === ''){
$('.isi').attr('placeholder','Masukkan Isi Pesan');
return false;
}
}
}
}