Jump to content

javascript number validation using regexp


sirup_segar

Recommended Posts

hi,i've been searching in this forum about my error, but still can't find it out.i try to use regexp to validate number only.

function cekqty(n)
  {
  var regcek=new RegExp(/^[0-9]+$/);
  for (i=0;i<n;i++)
  {
     var qty=document.getElementById("txt_qty"+i);
     var qty_ck=qty.value; 
if ((qty_ck<0) || (qty_ck.length==0) || (qty_ck.search(regcek)<0))
 {
  alert("Check Your Book Quantity");
  return false;
 }
 else 
 {
 return true;
 }
  }
} 

 

my form code is :

<form method='post' action='shop_action.php' name='frmshopaction' onSubmit='return cekqty($count)'>

 

my td code is :

<td><input type='text' name='txt_qty[$i]' id='txt_qty[$i]' value='$tmpqty' size='1'></td>

 

and my submit image is:

<input type='hidden' id='max_isbnx' value='$count'>
  <input type='image' src='../image/update-qty.gif' height='40' width='150' name='editshop' onClick='cekqty($count)'>

 

when i click the image, the error is "object required on line 44 char 3", that is on the "return false" statement. also, the error message displays twice, not only once,with the same error message.

lots lots thanks for your help...

hi, still working on my last thread, i found that when i click one of my submit image,

<input type='image' src='../image/lihat-katalog.gif' width='150' height='40' name='lihatkat'>
	     <input type='image' src='../image/update-qty.gif' height='40' width='150' name='editshop' onClick='cekqty($count)'>
		 <input type='image' src='../image/selesai-belanja.gif' width='150' height='40' name='cekout'>

the same error displayed,just like when i click the

<input type='image' src='../image/update-qty.gif' height='40' width='150' name='editshop' onClick='cekqty($count)'>

anyway, thanks for your attention..

mr rajiv, thanks for your attention. i've changed my code like you suggest me.

function cekqty()
  {
  var jml=document.getElementById("max_isbnx").value;
  for (i=0;i<jml;i++)
  {
     var qty=document.getElementById("txt_qty"+i); 
 if ((qty.value<0) || (qty.length==0) || (isNaN(qty)==true))
 {
  alert("Check Your Book Quantity");
  return false;
 }
 else 
 {
 return true;
 }
  }
} 

anyway, i've changed my form

<form method='post' action='shop_action.php' name='frmshopaction' onSubmit='return cekqty()'>

apparently the same error message still appear. ???

mr rajiv,i've solved the problem with my error. i change my code from

var qty=document.getElementById("txt_qty"+i); 

into

var qty=document.getElementById("txt_qty["+i+"]").value;

and it works, the error dissapear. but another problem come. when i click the submit image

<input type='image' src='../image/update-qty.gif' height='40' width='150' name='editshop' onClick='cekqty()'>

 

my form is submitted twice,not only once? so when i try to enter a negative number or non numeric char, the error message from cekqty() displayed twice. ???

thanks a lot.. :)

 

sorry mr rajiv, i did it again.. this is my cekqty()

function cekqty() 
{ 
    var jml=document.getElementById("max_isbnx").value; 
    for (i=0;i<jml;i++) 
     { 
      var qty=document.getElementById("txt_qty["+i+"]").value;  
      if ((qty<0) || (isNaN(qty)==true)) 
    	 { 
    	  alert("Periksa Kembali Inputan Jumlah Buku"); 
    	  return false; 
	  exit;
    	 } 
  }	 
     return true; 
    } 

 

and this is my form

<table align='center' width='650' class='mytable'>
	<tr>
	<form method='post' action='shop_action.php' name='frmshopaction' onSubmit='return cekqty()'>
		<td align=center><b>ISBN</b></td>
		<td align=center><b>Judul</b></td>
		<td align=center><b>Penerbit</b></td>
		<td align=center><b>Pengarang</b></td>
		<td align=center><b>Harga Satuan</b></td>
		<td align=center><b>Qty</b></td>
		<td align=center><b>Harga</b></td>
	 </tr>
";
$jml=0;
$price=0;
for ($i=0;$i<$hitung;$i++)
{
   $jml=$jml+$qtyx[$i];
   $price=$price+$hrg_qtyx[$i];
}
	for ($i=0;$i<$hitung;$i++)
	{
	$tmpisbn=$isbnx[$i];
	$tmpjdul=htmlspecialchars($judulx[$i]);
	$tmppen=$penerbitx[$i];
	$tmppeng=htmlspecialchars($pengarangx[$i]);
	$tmpqty=$qtyx[$i];
	$tmphrg=$hrgx[$i];
	$tmphrgtot=$hrg_qtyx[$i];
	$hrgrp=tampilrupiah($tmphrg);
	$hrgtot=tampilrupiah($tmphrgtot);
	$pricerp=tampilrupiah($price);
    	echo "<tr>
			 <td>$tmpisbn</td>
			 <td>$tmpjdul</td>
			 <td>$tmppen</td>
			 <td>$tmppeng</td>
			 <td>$hrgrp</td>
			 <td><input type='text' name='txt_qty[$i]' id='txt_qty[$i]' value='$tmpqty' size='1'></td>
			 <td>$hrgtot</td>
		 </tr>
  ";
}	
echo "<tr>
       <td colspan=8><div class='batalbeli'>Total Barang : <b>$jml</b> ‚ Total Harga : <b>$pricerp</b></div></td>
     </tr>
</table>";
echo "<table align='center'>
      <tr>
	    <td colspan='3'>
	      <div class='batalbeli'>Untuk membatalkan barang, masukkan angka 0 pada jumlah barang</div>
	    </td>
	  </tr>
	  <tr>
	    <td align='center'>
		     <input type='hidden' id='max_isbnx' value='$hitung'>
	         <input type='image' src='../image/update-qty.gif' height='40' width='150' name='editshop' onClick='cekqty()'>
		</form> 

thanks in advance..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.