Jump to content

[SOLVED] how to add date format validation to input form.


saw

Recommended Posts

Hello, I working with dreamweaver on a xampp winxp machine.

I am trying to learn mysql and php. I appreciate any advice or help anyone can give.

 

I Have a insert record form. I would like to make sure the user inputs a date in mm-dd-yyyy format.

 

below is the code I have for this little form so far (without validation)

Again thank you for looking.

Saw

 

<?php require_once('Connections/Insert.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO litter_num (Dob, Mother, Father) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['Dob'], "text"),
                       GetSQLValueString($_POST['Mother'], "text"),
                       GetSQLValueString($_POST['Father'], "text"));

  mysql_select_db($database_Insert, $Insert);
  $Result1 = mysql_query($insertSQL, $Insert) or die(mysql_error());
}
?><style type="text/css">
<!--
.style2 {
font-size: 36px;
font-weight: bold;
}
-->
</style>
<div align="center"><span class="style2">Create a New Litter Number</span></div>
<p> </p>

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Dob:</td>
      <td><input type="text" name="Dob" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Mother:</td>
      <td><input type="text" name="Mother" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Father:</td>
      <td><input type="text" name="Father" value="" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right"> </td>
      <td><input type="submit" value="Insert record" /></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p> </p>

 

 

Link to comment
Share on other sites

I can't seem to find the date variable there, but anyway the approach to this can be using:

 

<?php
$date = '09-03-2008';
list($month, $day, $year) = explode('-', $date);
if(!checkdate($month, $day, $year)){
   echo 'The date is invalid';
}
?>

 

It will check if the date is a normal one and not something like "45-78-99999". But it won't check for the date format, instead you can add such a thing:

<?php
$date = '09-03-2008';
if(!strtotime($date)){
    echo 'The date is invalid';
}
?>

Link to comment
Share on other sites

willpower..Perhaps I dont understand but are you saying I should use a drop down box for the dob, I dont see how that would work as I need to be able to have many years worth of dates accepted. to many options for a drop down?

 

--

 

GuiltyGear . Thank you for the code example. I am not sure where to add it though, I have tried adding it directly after the GetSQLValueString($_POST['Dob'], "text"), 

but that only results in a error.

 

thank you

saw

Link to comment
Share on other sites

If you have written that code by yourself, you should know where to place it. Otherwise, i'd suggest to start writing some code from scratch as it's the only way to learn. I don't know what the GetSQLValueString() custom function does and your code has too many not needed lines imo, so i'll just suggest the normal way of doing it:

 

<?php
if(isset($_POST['submit'])){
     $date = $_POST['date'];
     list($month, $day, $year) = explode('-', $date);
     if(!strtotime($date)){
          echo 'The date format is invalid. It should be: mm-dd-yyyy';
     } elseif(!checkdate($month, $day, $year)){
          echo 'The date is invalid. Either the month, day or year are of a not normal range.';
     }
}
?>

Link to comment
Share on other sites

I did not write that code myself, I use dreamweaver or rather I am trying to learn to use dreamweaver...I just started on this whole learn mysql php html project 3 days ago...I just want to learn enough to do this one project(or at least thats how it starts lol)

 

I have been trying to use the code sample you provided, but just cant seem to figure it out.

I have tried pasting it after the first instance of dob, I have tried pasting it in the form directly after dob and I have tried top and bottom of the page but nothing seems to work :(

 

Thank you for your patience, sorry I am so new to this.

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.