Tag: jquery

Membatasi jumlah kata dengan jquery

$("input[name='limited_input']").keyup(function(e) {
var text = $(this).val();
if (this.value.match(/[^a-zA-Z0-9 ]/g)) {//ijinkan karakter aplhanumerik saja
var length_txt = text.length-1;
$(this).val(text.substring(0,length_txt));
}
if (e.keyCode == 32) { //kode tombol utk 'spasi'
//batasi hanya 2 kata saja, setelahnya hilangkan
if((1*$(this).val().split(' ').length) > 3){
text = text.split(' ',3);
$(this).val(text.join(' '));
}
}
});

.reset() form jQuery

Lakukan:


$('#idForm')[0].reset();

nilai pada input didalam form dengan id=’idForm’ di reset.

Source code nya:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type='text/javascript' src='jquery.js'></script>
<script type='text/javascript'>
$(document).ready(function(){
$('#b_refresh').click(function(){
$('#idForm')[0].reset();
});
});
</script>
</head>
<body>
<form id='idForm'>
Nama: <input type='text' name='nama' value=''><br>
Alamat: <input type='text' name='nama' value=''><br>
No. Telpon: <input type='text' name='nama' value=''><br>
Gaji: <input type='text' name='nama' value=''><br>
<input type='submit' value='Go'>&nbsp;<input type='button' id='b_refresh' value='Reset'>
</form>
</body>
</html>

Demo disini.