Quick Validation Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Quick Validation Question

Post 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?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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)
Seattlebadboy
Forum Newbie
Posts: 11
Joined: Mon Jun 16, 2003 3:55 am
Location: Seattle, WA
Contact:

Post 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.
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post 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>
Post Reply