 |
| Javascript How-To Ask your "How do I do this with Javascript?" questions here. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 4th, 2005, 06:40 AM
|
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
date validation (again)
How do i stop someone entering a duff value then just hitting the 'enter' key to submit the form?
there is javascript, to ensure you enter in the correct format. but only if you tab to the button or click it.
function check_date(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
err = 0;
DateValue = DateField.value;
/* Delete all chars except 0..9 */
for (i = 0; i < DateValue.length; i++) {
if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
DateTemp = DateTemp + DateValue.substr(i,1);
}
}
DateValue = DateTemp;
/* Always change date to 8 digits - string*/
/* if year is entered as 2-digit / always assume 20xx */
if (DateValue.length == 6) {
DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
if (DateValue.length != 8) {
err = 19;}
/* year is wrong if year = 0000 */
year = DateValue.substr(4,4);
if (year == 0) {
err = 20;
}
/* Validation of month*/
month = DateValue.substr(2,2);
if ((month < 1) || (month > 12)) {
err = 21;
}
/* Validation of day*/
day = DateValue.substr(0,2);
if (day < 1) {
err = 22;
}
/* Validation leap-year / february / day */
if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
leap = 1;
}
if ((month == 2) && (leap == 1) && (day > 29)) {
err = 23;
}
if ((month == 2) && (leap != 1) && (day > 28)) {
err = 24;
}
/* Validation of other months */
if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
err = 25;
}
if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
err = 26;
}
/* if 00 ist entered, no error, deleting the entry */
if ((day == 0) && (month == 0) && (year == 00)) {
err = 0; day = ""; month = ""; year = ""; seperator = "";
}
/* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
if (err == 0) {
DateField.value = day + seperator + month + seperator + year;
}
/* Error-message if err != 0 */
else {
alert("Incorrect Date: Please Enter the Date in Format 'DD/MM/YY'");
DateField.select();
DateField.focus();
}
}
thats my existing javascript.
any ideas?
Picco
|
|

February 4th, 2005, 06:43 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
|
|
How are you calling the check_date() function?
Cheers,
Chris
|
|

February 4th, 2005, 06:45 AM
|
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
My textbox calls it:
<input type="text" size="10" name="date" value="" onblur="check_date(this)">
Should this work?
|
|

February 4th, 2005, 06:58 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
|
|
You can perform validation when the form is submitted using the forms onsubmit event...
Code:
<form name="myFormName" onsubmit="return check_date(this.date);">
You will also need to update your function to return true / false depending on whether or not the field passes validation to prevent the form submitting.
HTH,
Chris
|
|

February 4th, 2005, 07:02 AM
|
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
where would i add this true/false statement?
thanks
|
|

February 4th, 2005, 07:09 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
|
|
By the look of your function you are setting an error code & if this code is 0, everything is ok.
So at the end of your function add...
Code:
return err == 0 ? true : false;
|
|

February 4th, 2005, 07:12 AM
|
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
chris,
--------------------------------------------------------------------------------
http://www.donaldmackenzie.com/
Does anyone know of a font face (or name) like the text 'Welcome to Mackenzie Travel' at the top of this page pasted above?
Thanks.
Picco
|
|

February 4th, 2005, 07:56 AM
|
|
Friend of Wrox
|
|
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
cheers. went through 20-odd questions.....
and you have to BUY the font!!
www.crmpicco.co.uk
|
|

February 4th, 2005, 08:27 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Quote:
quote:Originally posted by crmpicco
cheers. went through 20-odd questions.....
and you have to BUY the font!!
www.crmpicco.co.uk
|
Amazing, someone wants paying for all the work they've done :)
--
Joe ( Microsoft MVP - XML)
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| Date Validation |
aftabn10 |
PHP How-To |
0 |
January 29th, 2007 10:35 AM |
| Date validation |
vacak |
Struts |
0 |
November 21st, 2006 12:11 PM |
| Date Validation |
ramesh055 |
ASP.NET 1.0 and 1.1 Professional |
1 |
November 15th, 2006 11:09 AM |
| Date Validation |
surendran |
Javascript How-To |
3 |
February 25th, 2005 07:40 AM |
| Date validation |
Raul |
Javascript |
4 |
February 25th, 2004 04:04 PM |
|
 |