Jump to content

[SOLVED] Form Validation Issue


macattack

Recommended Posts

I have written a messy and likely very inefficient code to validate a form in PHP. Unfortunately, no matter what I put in for values, it returns that the year is invalid, and stops the process.

 

Also, can anyone suggest how to check to make sure the date isn't in the future?

 

Thanks in advance!

 

The code begins with elseif because it's the second form to be validated. Form one works.

elseif($_GET['addold'] == 'Add Old'){
	if(!strlen($_POST['year'] == '4')){
		echo "Year is invalid";
		echo $old;
	}
	elseif(!is_numeric($_POST['year'])){
		echo "Year must be numeric";
		echo $old;
	}
	elseif(!strlen($_POST['month'] == '2')){
		echo "Month is invalid";
		echo $old;
	}
	elseif($_POST['month'] > '12'){
		echo "Month is invalid";
		echo $old;
	}
	elseif(!is_numeric($_POST['month'])){
		echo "Month must be numeric";
		echo $old;
	}
	elseif(!strlen($_POST['day'] == '2')){
		echo "Day is invalid";
		echo $old;
	}
	elseif(!$_POST['day'] < '31'){
		echo "Day is invalid";
		echo $old;
	}
	elseif(!is_numeric($_POST['day'])){
		echo "Day must be numeric";
		echo $old;
	}
	elseif(!strlen($_POST['title'] == "")){
		echo "Title is invalid";
		echo $old;
	}
	else{
		displayAddOldBlogForm();
	}
}

 

and here is the form in question

 

$old = '<form action="index.php?addold=Add Old" method="POST">
		<label for="title">Title:</label><input type="text" name="title"><br>
		<label for="year">Year (YYYY):</label><input type="text" name="year" maxlength="4"><br>
		<label for="month">Month (MM):</label><input type="text" name="month" maxlength="2"><br>
		<label for="day">Day (DD):</label><input type="text" name="day" maxlength="2"><br>
		<input type="submit" value="Write post for the specified date with this name"><br>
	</form>';

Link to comment
Share on other sites

What will happen if the user enters 0000-00-00 for the design of your date pattern?

You need to check if the date is some valid one.

To get that the entered date is not that of the future one, get the system date using functions like now() and then get the posted year and compare these two values.

You can manipulate in any ways as you like.

It will be better for the months and the days to have some selection fields rather than the text field and then checking for the length.

For the year also it can be the selection field but it may have some large entries so text field is also good but be sure that the user enters the date pattern as you desire.

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.