Jump to content

Need some help implementing Page Validation


timcadieux

Recommended Posts

I have a Search page that has some date fields that are NOT mandatory.  However, if someone fills any of the Date fields (YYYY or MM or DD), I need to be certain that they've filled in all three.

Any ideas?

			<div style="margin-top:10px;width:350px;" id="SpecificDateDiv" name="SpecificDateDiv">
			 <!--Dates-->
				 <label for="DoD_YYYY" style="width:175px;">Specific Date:</label>
				 <input id="DoD_YYYY" type="text" name="DoD_YYYY" value="" style="width:35px;"/> - 
				 <input id="DoD_MM" type="text" name="DoD_MM" value="" style="width:23px;"/> -                 
				 <input id="DoD_DD" type="text" name="DoD_DD" value="" style="width:23px;"  />   
				 <a class='info' title="Search for a known Date of Death, please use indicated format YYYY-MM-DD.  If you are unsure, use the Date Range field."><img src='images/grey_question-mark.gif' height='14'></a>				
			 </div>	
Link to comment
Share on other sites

This should really be a two step process. Once to verify that if any fields are entered that they all have values. Then you would need to validate that the value make a valid date.

 

 

$day = isset($_POST['DoD_DD']) ? trim($_POST['DoD_DD']) : '';
$month = isset($_POST['DoD_MM']) ? trim($_POST['DoD_DD']) : '';
$year = isset($_POST['DoD_YY']) ? trim($_POST['DoD_DD']) : '';

//Verify if ANY date fields have a value
if(!empty($day) || !empty($month) || !empty($year))
{
    //verify that ALL date fields have a value
    if(!empty($day) || !empty($month) || !empty($year))
    {
        echo "You must enter a value in all date field";
    }

    //Perform validation that the date fields values are a VALID date
}
Link to comment
Share on other sites

I implemented as below, but it fires even when i just ht Search, without having entered and Dates at all..?

<?php

$day = isset($_POST['DoD_DD']) ? trim($_POST['DoD_DD']) : '';
$month = isset($_POST['DoD_MM']) ? trim($_POST['DoD_DD']) : '';
$year = isset($_POST['DoD_YY']) ? trim($_POST['DoD_DD']) : '';
$errMsg ='';

//Verify if ANY date fields have a value
	if(!empty($day) || !empty($month) || !empty($year))
	{
		//verify that ALL date fields have a value
		if(!empty($day) || !empty($month) || !empty($year))
		{
			$errMsg = "You must enter a value in all date field";
		}
		else
		{
			header ("Location: results.php"); exit;
		}

		//Perform validation that the date fields values are a VALID date
	}

?>

Link to comment
Share on other sites

Sorry copy/paste error. Forgot to remove the "!"'s from the second condition check

//Verify if ANY date fields have a value
if(!empty($day) || !empty($month) || !empty($year))
{
    //verify that ALL date fields have a value
    if(empty($day) || empty($month) || empty($year))
    {
        $errMsg = "You must enter a value in all date field";
    }
    else
    {
        header ("Location: results.php"); exit;
    }

    //Perform validation that the date fields values are a VALID date
}
 

But, I'm not sure I understand the purpose of the header(). With what you have now, if the user has entered values for all of the date fields they will be redirected and all the data lost!

Edited by Psycho
Link to comment
Share on other sites

Actually, this isn't working but I can't seem to fix it...the issuie stems from a JQuery that i'm using to add a WaterMark to the 3 fields...therefore they never actually resolve to empty as the $day has 'DD', $year = 'YYYY' and so on.

 

I've tried adding && $day !='DD' but then nothing happens at all when I submit.

 

So, I need to be able to submit if everythign is blank, however, if any of the Dates fields is not blank, I need to stop and produce an error.  I'm not sure why i DID work for me for a while.  If I disable the Jquery, it works just fine, but obviously I'd like to keep it.

	jQuery(function ($) {

            $("#DoD_YYYY").Watermark("YYYY", "#dfdae0");
        });
$day = isset($_POST['DoD_DD']) ? trim($_POST['DoD_DD']) : '';
$month = isset($_POST['DoD_MM']) ? trim($_POST['DoD_MM']) : '';
$year = isset($_POST['DoD_YYYY']) ? trim($_POST['DoD_YYYY']) : '';
$errMsg ='';

//Verify if ANY date fields have a value
	if(!empty($day) or !empty($month) or !empty($year))
	{
		//verify that ALL date fields have a value
		if(empty($day) or empty($month) or empty($year))
		{
			$errMsg = "You must enter a value in all date field";
		}
		else
		{
			header ("Location: view-paginated.php?$day$month$year"); exit;
		}

		//Perform validation that the date fields values are a VALID date
	}
Link to comment
Share on other sites

You could do two things:

 

1. If the values received in PHP are the same as the prompt text, change them to an empty string.

2. Use JavaScript to implement an onsubmit() trigger to remove the values in those fields if they are equal to the prompt text

 

I would go with the latter because you could always ending up changing the prompt text. Keeping all the functionality with the prompt text in one place will make maintenance easier.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.