Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RESTRICTING USER INPUT 1

Status
Not open for further replies.

sciclunam

Programmer
Oct 12, 2002
138
MT
Hi,

I have a form where it accepts a date inputted by users. I need users to input ONLY in dd-mm-yyyy format. What is the best way to do it?

I was thinking of having 3 list boxes for the days, months and years. But I need to check the inputted dates for invalid entries like for eg: 31-02-2004.

Is there some some kind of drop down calendar which takes care of this?

Thanks

Mario

Getting married in Malta?
Visit
 
Mario,

I tend to use the drop down list method all of the time, as I just like the look of it to be honest. If I had the time i would write my own calendar JS app, as all of the ones out there have never done all of what I want them to be able to do.

I use the following javascript to validate the date is a valid date:

Code:
function prepare(fDay, fMonth, fYear, FormDayName, FormName) {
	if (checkDate(fDay, fMonth, fYear) == false) {
		document[FormName].elements[FormDayName].value = daysinmonth;
	}
}

//Functions for checking for a valid date
var daysinmonth;
function checkDate(day, month, year) {
	daysinmonth = 31;
	if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
		daysinmonth = 30
	}
	yearby4 = eval(year / 4);
	yearby4mod = parseInt(yearby4);
	if ((month == '2') && (yearby4 == yearby4mod)) {
		daysinmonth = 29
	}
	if ((month == '2') && (yearby4 != yearby4mod)) {
		daysinmonth = 28
	}
	if (day > daysinmonth) {
		alert ('Sorry, that is an invalid date.\nThe date will change to the nearest correct date.');
		return false;
	}
	else return true;
}

then on my form I have this:

Code:
<select name="ExpiryDay" onChange="prepare(document.staff.ExpiryDay.value,document.staff.ExpiryMonth.value,document.staff.ExpiryYear.value,'ExpiryDay','staff')">
<option>php output</option>
</select>

the prepare function takes the values of the three elements on the page (Day, Mopnth and year) and the name of the FormField to change, in this case the Expiry day and the name of the form.

If you want to post an email address up here, i'll send you the code in full

Hope this helps!

Tony
 
Hi aperry,

That is exactly what I need. I have pasted the top part of the script as javascript and pasted the other part in the form.

Whilst I got no error I ended with a drop down having "php output" in the list.

I assume the code is not complete as you offered to email me the full code. I would appreciate if you send it. Sorry, but I am literally new to PHP. MY email is mario@theweddingsite.com

Thanks

Mario


Getting married in Malta?
Visit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top