Page 1 of 1

Quick Validation Question

Posted: Thu Jan 13, 2005 3:47 pm
by mmc01ms
Im trying to validate some forms one is where the value of the cat_no has to be 6 numbers my code below doesn't validate it. If i enter two values it just enters it into the database.

Code: Select all

if(strlen($catno < 6)) error_message("6 Digit Value Needed");

any ideas how i could improve this or any other ways to do it?

Posted: Thu Jan 13, 2005 3:52 pm
by rehfeld

Code: Select all

if (strlen($catno) != 6 || !ctype_digit($catno)) &#123;
    // error
&#125; else &#123;
    // ok
&#125;
that would make sure $catno is exactly 6 chars long, and that every single char is a number


you had
if (strlen($catno < 6))
should have been
if (strlen($catno) < 6)

Posted: Thu Jan 13, 2005 3:54 pm
by Seattlebadboy
I think the reason it isn't working, is because you're entering numbers as values. Your code is check for the string length numbers are not a string.

Posted: Fri Jan 14, 2005 4:48 pm
by mmc01ms
cheers guys great help another problem regarding this is my qty check error message. I have entered all required feilds however the code

Code: Select all

if(empty($qty)) error_message("No Qty Entered");
keeps excuting when it should not? So it wont let me move on! Any ideas full code is below

Code: Select all

<?php
	session_start();
	
	require('page.inc');
	require_once('update_delete_fns.php');
	
	$editform = new Page();
	
	$editform -> Display();
	
	$catno = $_POST&#1111;'cat_no'];
	$title = $_POST&#1111;'title'];
	$artist = $_POST&#1111;'artist_id'];
	$release_date = $_POST&#1111;'release_date'];
	$genre = $_POST&#1111;'genre_ref'];
	$price = $_POST&#1111;'price'];
	$qty = $_POST&#1111;'qty'];
	$info = $_POST&#1111;'comments'];
	$oldcatno = $_POST&#1111;'oldcatno'];
	
	
	if(empty($title)) error_message("No Title Entered");
	if(empty($price)) error_message("No Price Entered");
	if(empty($qty)) error_message("No Qty Entered");
	if(empty($info)) error_message("Please Enter some Information about the vinyl");
	if (strlen($catno) != 6 || !ctype_digit($catno)) error_message("Catalogue value needs to be 6 digits"); 
   


	
 	if (filled_out($_POST))
	&#123;
	
	$catno = $_POST&#1111;'cat_no'];
	$title = $_POST&#1111;'title'];
	$artist = $_POST&#1111;'artist_id'];
	$release_date = $_POST&#1111;'release_date'];
	$genre = $_POST&#1111;'genre_ref'];
	$price = $_POST&#1111;'price'];
	$qty = $_POST&#1111;'qty'];
	$status = $_POST&#1111;'status_code'];
	$info = $_POST&#1111;'comments'];
	$oldcatno = $_POST&#1111;'oldcatno'];
	
	if (update_vinyl($oldcatno, $catno, $title, $artist, $release_date, $genre, $price, $qty, $status, $info))
		&#123;
			echo 'vinyl was updated.<br />';
		&#125;
		
    else
      echo 'vinyl could not be updated.<br />';
  &#125; 
	
	
	do_html_footer();
	
	
	
?>

also the form the qty comes from the entry field to enter the qty amount is

Code: Select all

<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Qty: </font></td>
					<td><input name="qty" type="text" maxlength="3" id="qty" value="<?php echo $edit?$vinyl&#1111;'stock_level']: ' '; ?>"></td>
				</tr>