Jump to content

Help checking inputs


TOA

Recommended Posts

I used this tutorial to build a script.

 

http://www.phpfreaks.com/tutorial/php-basic-database-handling

 

Basically, without reading the tutorial, here's what it does: allows you to enter/update db entries via an html table. (very useful..that you crayon violent) When submitted, it checks to see if anything has been entered in the new input or updated in the previous inputs and acts accordingly.

 

I also have this js function, that checks for a valid date format and age restrictions.

<script type="text/javascript">
function checkAge(input)
{ 
var today = new Date(); 
var d = document.getElementById(input).value;
if (d == "")
{
	showMessage('empty',input);
	return false;
}

if (!checkFormat(input)) 
{ // check valid format
	return false;
}

d = d.split("-");
var byr = parseInt(d[0]); 
var nowyear = today.getFullYear();
if (byr >= nowyear || byr < 1900) 
{ // check valid year
	showMessage('impossible',input);
	return false;
}
var bmth = parseInt(d[1],10)-1; // radix 10!
if (bmth <0 || bmth >11) 
{ // check valid month 0-11
	showMessage('impossible',input); 
	return false;
}
var bdy = parseInt(d[2],10); // radix 10!
var dim = daysInMonth(bmth+1,byr);
if (bdy <1 || bdy > dim) 
{ // check valid date according to month
	showMessage('impossible',input);
	return false;
}

var age = nowyear - byr;
var nowmonth = today.getMonth();
var nowday = today.getDate();
var age_month = nowmonth - bmth;
var age_day = nowday - bdy;
if (age < 18 )
{
	showMessage('child',input);
	return false;
}
else if (age == 18 && age_month <= 0 && age_day <0)
{
	showMessage('child',input);
	return false;
}
}

function checkFormat(input)
{
var validformat = /\d{4}\-\d{2}\-\d{2}/;
var d = document.getElementById(input).value;
if (!validformat.test(d)) 
{ // check valid format
	showMessage('format',input);
	return false;
}
return true;
}

function showMessage(messagetype, input) 
{
if (messagetype == "format") 
{
	alert ("Invalid date format - please re-enter as YYYY-MM-DD");
}
else if (messagetype == "impossible")
{
	alert ("Impossible year/month/day of birth - please re-enter.");		
}
else if (messagetype == "child")
{
	alert ("Only adults are allowed.");		
}
else if (messagetype == "empty")
{
	alert ("Nothing was filled out.");		
}
document.getElementById(input).select();
}

function daysInMonth(month,year) { // months are 1-12
var dd = new Date(year, month, 0);
return dd.getDate();
}
</script>

 

As you can see, I call it with the id of the input I want to check.

 

What heppens currently is that it always calls back the empty error when updating a previous entry, because it's only checking the one input.

What I need to do is use it to iterate through all the date fields in the form, and if the new input is not filled out, skip it.

Hope that makes sense.

 

Any suggestions?

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.