var text = '50';
var reg = /0/;
if(reg.test(text) || parseInt(text) > 100){
alert('invalid data')
}
kalau 50 boleh:if(parseInt(text)<= 0 && parseInt(text) > 100){
alert('invalid data');
}
<form name="form2" id="form2" method="post" action="">
<table>
<tr>
<td>Resolusi</td>
<td></td>
<td><input name="resolusi" id="resolusi" type="text" size="5" maxlength="3" /> %</td>
</tr>
<tr>
<td>Harga</td>
<td></td>
<td><input name="harga" id="harga" type="text" size="5" maxlength="3" /> %</td>
</tr>
<tr>
<td>Jumlah Auto-Fokus</td>
<td></td>
<td><input name="af" id="af" type="text" size="5" maxlength="3" /> %</td>
</tr>
<tr>
<td>Ukuran Sensor</td>
<td></td>
<td><input name="sensor" id="sensor" type="text" size="5" maxlength="3" /> %</td>
</tr>
<tr>
<td></td>
<td><input name="ok" type="button" value="OK" onclick="validate(); return false;"></td>
<td><input name="reset" type="Reset" value="Reset" /></td>
</tr>
</table>
</form>
<!-- tambahkan javascript --->
<script type="text/javascript">
function validate(){
var resolusi = document.getElementById('resolusi').value;
if(parseInt(resolusi)<= 0 || parseInt(resolusi) > 100){
alert('Invalid data resolusi');
return false;
}
var harga = document.getElementById('harga').value;
if(parseInt(harga)<= 0 || parseInt(harga) > 100){
alert('Invalid data harga');
return false;
}
var af = document.getElementById('af').value;
if(parseInt(af)<= 0 || parseInt(af) > 100){
alert('Invalid data resolusi');
return false;
}
var sensor = document.getElementById('sensor').value;
if(parseInt(sensor)<= 0 || parseInt(sensor) > 100){
alert('Invalid data resolusi');
return false;
}
document.getElementById("form2").submit();
}
</script>
sebagai catatan kode php tidak bisa dipanggil dari javascript, yang ada adalah php digunakan untuk membentuk javascript dan tag html lainnya secara dinamis. <script type="text/javascript">
function validate(){
var resolusi = document.getElementById('resolusi').value;
if(parseInt(resolusi)<= 0 || parseInt(resolusi) > 100 || /\D/.test(resolusi)){
alert('Invalid data resolusi');
return false;
}
var harga = document.getElementById('harga').value;
if(parseInt(harga)<= 0 || parseInt(harga) > 100 || /\D/.test(harga)){
alert('Invalid data harga');
return false;
}
var af = document.getElementById('af').value;
if(parseInt(af)<= 0 || parseInt(af) > 100 || /\D/.test(af)){
alert('Invalid data Auto FOcus');
return false;
}
var sensor = document.getElementById('sensor').value;
if(parseInt(sensor)<= 0 || parseInt(sensor) > 100 || /\D/.test(sensor)){
alert('Invalid data sensor');
return false;
}
document.getElementById("form2").submit();
}
</script>
function cekinput(){
var resolusi = document.getElementById('resolusi').value;
if(isNaN(parseInt(resolusi)) || parseInt(resolusi)<= 0 || parseInt(resolusi) > 100){
alert('Input bobot 1-100 untuk kriteria resolusi');
document.form2.resolusi.focus();
return false;
}
var harga = document.getElementById('harga').value;
if(isNaN(parseInt(harga)) || parseInt(harga)<= 0 || parseInt(harga) > 100){
alert('Input bobot 1-100 untuk kriteria harga');
document.form2.harga.focus();
return false;
}
var af = document.getElementById('af').value;
if(isNaN(parseInt(af)) || parseInt(af)<= 0 || parseInt(af) > 100 ){
alert('Input bobot 1-100 untuk kriteria auto fokus');
document.form2.af.focus();
return false;
}
var sensor = document.getElementById('sensor').value;
if(isNaN(parseInt(sensor)) || parseInt(sensor)<= 0 || parseInt(sensor) > 100){
alert('Input bobot 1-100 untuk kriteria sensor');
document.form2.sensor.focus();
return false;
}
document.getElementById("form2").submit();
}