JavaScript – Cek field kosong (1 field)

function cek(obj){
var str=encodeURI(document.getElementById("keyword").value);
if(str==null || str==""){
alert("Field Kosong!");
}
else {
obj.submit();
}
}

Fungsi javascript di atas fungsinya adalah untuk mengecek apakah field tersebut memuat karakter atau tidak, jika tidak ia tidak akan memproses.
Dapat digunakan untuk membuat widget pencarian. Cara penggunaan script tersebut :

<form id=”namaform” name=”namaform” method=”post” action=”?link=search_result”>
Searh <input type=”text” name=”keyword” id=”keyword” />
<input type=”submit” name=”Submit” value=”Cari” onclick=”javascript:cek(document.getElementById(‘namaform’));return false;” />
</form>

Selamat mencoba.

About Iin aryani

I don't wanna be a "Copycat" but, something they need to copy.

Posted on 11 July 2011, in Website. Bookmark the permalink. Leave a comment.

Komentar untuk penulis

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: