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)) {
// error
} else {
// ok
}
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ї'cat_no'];
$title = $_POSTї'title'];
$artist = $_POSTї'artist_id'];
$release_date = $_POSTї'release_date'];
$genre = $_POSTї'genre_ref'];
$price = $_POSTї'price'];
$qty = $_POSTї'qty'];
$info = $_POSTї'comments'];
$oldcatno = $_POSTї'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))
{
$catno = $_POSTї'cat_no'];
$title = $_POSTї'title'];
$artist = $_POSTї'artist_id'];
$release_date = $_POSTї'release_date'];
$genre = $_POSTї'genre_ref'];
$price = $_POSTї'price'];
$qty = $_POSTї'qty'];
$status = $_POSTї'status_code'];
$info = $_POSTї'comments'];
$oldcatno = $_POSTї'oldcatno'];
if (update_vinyl($oldcatno, $catno, $title, $artist, $release_date, $genre, $price, $qty, $status, $info))
{
echo 'vinyl was updated.<br />';
}
else
echo 'vinyl could not be updated.<br />';
}
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ї'stock_level']: ' '; ?>"></td>
</tr>