Jump to content


Photo

0 Not recognised as php required field imput?


  • Please log in to reply
10 replies to this topic

#1 Captain Salad

Captain Salad
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 27 March 2006 - 02:20 PM

Sorry to bug this forum with yet more of my woes but for some reason when I test my form, if I add a 0 (just a zero, nothing more) to a required field it doesnt pick it up and states that I missed a required field.

Any help on this one? Thanks you for any time and help,

God bless




#2 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 27 March 2006 - 02:35 PM

[a href=\"http://uk2.php.net/empty\" target=\"_blank\"]http://uk2.php.net/empty[/a]

The string '0' is considered empty, so will have to use a different method of validation if you want to allow zeros.
Personal site: NewEnigma  |  Java and JSF Discussion

#3 Guest_footballkid4_*

Guest_footballkid4_*
  • Guests

Posted 27 March 2006 - 02:42 PM

You can use:
if ($_REQUEST['email'] == "")
{
     //not supplied
}

Or
if ( strlen( trim( $_REQUEST['email'] ) ) > 0 )
{
    //supplied
}


#4 Captain Salad

Captain Salad
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 27 March 2006 - 02:43 PM

[!--quoteo(post=358901:date=Mar 27 2006, 09:35 AM:name=annihilate)--][div class=\'quotetop\']QUOTE(annihilate @ Mar 27 2006, 09:35 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
[a href=\"http://uk2.php.net/empty\" target=\"_blank\"]http://uk2.php.net/empty[/a]

The string '0' is considered empty, so will have to use a different method of validation if you want to allow zeros.
[/quote]

Thanks for your input, I was afraid of that. Any help with the code I would need to add in order to allow 0? I am a total newb to PHP and coding of any sort so any further help will be greatly appreciated.

God bless


#5 Captain Salad

Captain Salad
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 27 March 2006 - 02:56 PM

[!--quoteo(post=358904:date=Mar 27 2006, 09:42 AM:name=footballkid4)--][div class=\'quotetop\']QUOTE(footballkid4 @ Mar 27 2006, 09:42 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You can use:
if ($_REQUEST['email'] == "")
{
     //not supplied
}

Or
if ( strlen( trim( $_REQUEST['email'] ) ) > 0 )
{
    //supplied
}
[/quote]


Hi, Thank you for trying to help me, so would this be correct?

$yearsataddress = if ($_REQUEST['yearsataddress'] == "")
{
//not supplied
}
$monthsataddress = $_REQUEST['monthsataddress'] ;

#6 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 27 March 2006 - 03:00 PM

Try this, substitute the function empty in your script for checkLength
Example use: checkLength($title, 0, 50);
// String length
// Usage:  checkLength(string to check length of, min length, max length)
function checkLength($string, $min, $max) 
{
$length = strlen(trim($string));
    if ( ($length < $min) || ($length > $max) ) 
    {
    return FALSE;
    } 
    else
    {
    return TRUE;
    }
}

Personal site: NewEnigma  |  Java and JSF Discussion

#7 Captain Salad

Captain Salad
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 27 March 2006 - 04:32 PM

Hi, thanks for you help, sadly I think I am way over my head as I cant get it to work still and not sure wher I should add the code (as I said I am total noob) , is there any chance you could be so kind as to amend the below code so that the $yearsataddress field proceses 0 if entered?

Sorry for all these pleas for help.

God bless



#8 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 27 March 2006 - 04:42 PM

Put this anywhere on the page, I would put at the top though.
// String length
// Usage:  checkLength(string to check length of, min length, max length)
function checkLength($string, $min, $max) 
{
$length = strlen(trim($string));
    if ( ($length < $min) || ($length > $max) ) 
    {
    return FALSE;
    } 
    else
    {
    return TRUE;
    }
}

Then change this code section:
if (!isset($_REQUEST['email'])) {
header( "Location: http://www.brittalk.com/brittalk/Templates...actUs.htm" );
}
elseif (empty($email) || empty($transfer) || empty($title) || empty($firstname) || empty($lastname) || empty($dob) || empty($currentaddress)

To this (I would run the checkLength function on all fields)
if (!isset($_REQUEST['email'])) {
header( "Location: http://www.brittalk.com/brittalk/Templates...actUs.htm" );
}
elseif (checkLength($email,1,40) || checkLength($transfer,1,50) )
// etc etc etc
If you just want it on the yearsataddress field, then change empty($yearsataddress) to checkLength($yearsataddress, 1, 5) or something. The 1 is the min number of digits, and the 5 is the max. Just change those numbers depending on how long or short the field can be.
Personal site: NewEnigma  |  Java and JSF Discussion

#9 Captain Salad

Captain Salad
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 28 March 2006 - 10:16 AM

Hi, I changed the code to what you suggested but I get an "error missed a field" message for no matter what I type in the fields, have I missed something, see edited code below.

god bless

<?
function checkLength($string, $min, $max)
{
$length = strlen(trim($string));
if ( ($length < $min) || ($length > $max) )
{
return FALSE;
}
else
{
return TRUE;
}
}

$transfer = $_REQUEST['transfer'] ;
$email = $_REQUEST['email'] ;
$existingnumber = $_REQUEST['existingnumber'] ;
$pacnumber = $_REQUEST['pacnumber'] ;
$title = $_REQUEST['title'] ;
$firstname = $_REQUEST['firstname'] ;
$middleinitial = $_REQUEST['middleinitial'] ;
$lastname = $_REQUEST['lastname'] ;
$dob = $_REQUEST['dob'] ;
$currentaddress = $_REQUEST['currentaddress'] ;
$town = $_REQUEST['town'] ;
$postcode = $_REQUEST['postcode'] ;
$residentialstatus = $_REQUEST['residentialstatus'] ;
$yearsataddress = $_REQUEST['yearsataddress'] ;
$monthsataddress = $_REQUEST['monthsataddress'] ;
$previousaddress = $_REQUEST['previousaddress'] ;
$previoustowm = $_REQUEST['previoustown'] ;
$previouspostcode = $_REQUEST['previouspostcode'] ;
$hometel = $_REQUEST['hometel'] ;
$daytimetel = $_REQUEST['daytimetel'] ;
$bankaccountyears = $_REQUEST['bankaccountyears'] ;
$bankaccountmonths = $_REQUEST['bankaccountmonths'] ;
$occupationalstatus = $_REQUEST['occupationalstatus'] ;
$creditcard = isset($_POST['creditcard']) ? $_POST['creditcard'] : 0;
$yearsinjob = $_REQUEST['yearsinjob'] ;
$monthssinjob = $_REQUEST['monthsinjob'] ;
$accountholder = $_REQUEST['accountholder'] ;
$accountnumber = $_REQUEST['accountnumber'] ;
$branchsortcode = $_REQUEST['branchsortcode'] ;
$printnamebank = $_REQUEST['printnamebank'] ;
$datebank = $_REQUEST['datebank'] ;
$termsconditions = $_REQUEST['termsconditions'] ;
$printnameterms = $_REQUEST['printnameterms'] ;
$printdateterms = $_REQUEST['printdateterms'] ;

if (!isset($_REQUEST['email'])) {
header( "Location: [a href=\"http://www.brittalk.com/brittalk/Templates/contactUs.htm"\" target=\"_blank\"]http://www.brittalk.com/brittalk/Templates...actUs.htm"[/a] );
}
elseif (checkLength($email,1,50) || checkLength($transfer,1,50) || checkLength($title,1,50) || checkLength($firstname,1,50) || checkLength($lastname,1,50) || checkLength($dob,1,50) || checkLength($currentaddress,1,50)
|| checkLength($town,1,50) || checkLength($postcode,1,50) || checkLength($residentialstatus,1,50) || checkLength($yearsataddress,1,50) || checkLength($monthsataddress,1,50) || checkLength($hometel,1,50) || checkLength($daytimetel,1,50)
|| checkLength($bankaccountyears,1,50) || checkLength($bankaccountmonths,1,50) || checkLength($occupationalstatus,1,50) || checkLength($monthssinjob,1,50) || checkLength($yearsinjob,1,50) || checkLength($accountholder,1,50)
|| checkLength($accountnumber,1,50) || checkLength($branchsortcode,1,50) || checkLength($printnamebank,1,50) || checkLength($datebank,1,50) || checkLength($termsconditions,1,50) || checkLength($printnameterms,1,50) || checkLength($printdateterms,1,50)) {

#10 Captain Salad

Captain Salad
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 28 March 2006 - 10:36 AM

sorry double post

#11 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 28 March 2006 - 11:55 AM

I would change all the || into &&. And if you want to allow some fileds to be left blank, put the min value as 0. I have quickly done a test of whether it works and it works perfectly. This is the little bit of code I tested on.
<?php
function checkLength($string, $min, $max) 
{
$length = strlen(trim($string));
    if ( ($length < $min) || ($length > $max) ) 
    {
    return FALSE;
    } 
    else
    {
    return TRUE;
    }
}

$name = trim(strip_tags($_POST['Name'])); // Name
$email = trim($_POST['Email']); // Email
    
if ( checkLength($email, 1, 50) && checkLength($name, 1, 50))
{
echo 'Passed validation';
}
else
{
echo 'Failed';
}

?>
    
    <form name="Contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
      <table class="padform">
    <tr><td><label for="Name">Name</label></td><td><input name="Name" id="Name" type="text" size="50" maxlength="50" value="<?php if ( isset($_POST['Name']) ) echo trim(strip_tags($_POST['Name'])); ?>" /></td></tr>
    <tr><td><label for="Email">Email</label></td><td><input name="Email" id="Email" type="text" size="50" maxlength="50" value="<?php if ( isset($_POST['Email']) ) echo trim($_POST['Email']); ?>" /></td></tr>
    <tr><td> </td><td align="center"><input name="Submit" type="submit" value="Contact" /></td></tr>
    </table>
    </form>

Personal site: NewEnigma  |  Java and JSF Discussion




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users