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!

Javascript date validation on multiple fields

Status
Not open for further replies.

cwolgamott

Programmer
May 29, 2002
69
US
Hello. :) I am creating some asp pages. On one of the pages, a modification page, I am trying to perform a date validation function. I know the function works, but I have been unsuccessful at getting it to work on multiple fields. For example, if one of the text boxes is empty, it says that text box needs a value of MM/DD/YY. Here is how I am trying to call the function:

function checktheboxes() {
if (isDate(document.form1.installdate.value) == false) Or (document.form1.installdate.value != "")
{
document.form1.installdate.focus();
return (false);
}
if (isDate(document.form1.maintdate.value) == false) Or (document.form1.maintdate.value != "")
{
document.form1.maintdate.focus();
return (false);
}
if (isDate(document.form1.readdate.value) == false) Or (document.form1.readdate.value != "")
{
document.form1.readdate.focus();
return (false);
}

return true;
}

It works ok if there is only one text box to check, but not for multiple text boxes. I would greatly appreciate any suggestions or hints. I call this function in the form tag. Thanks. :)
 
Isn't "Or" in JavaScript "||" and wouldn't you want them to be "&&" (AND)?
 
This is how I'd do it:

function checktheboxes() {
f = document.form1;
for (n=0; n<f.elements.length; n++)
if (f.elements[n].type == &quot;text&quot; && !isDate(f.elements[n].value) && f.elements[n].value != &quot;&quot;)
{
f.elements[n].focus();
return (false);
}

return true;
}

Also, I didn't see such a function as isDate() in standard JS. You'll probably have to write your own or find a ready one.
 
Hello. :) Thank you so much for your response. I tried the code that you suggested but am unable to get it to work correctly. My page actually includes more than just the three date fields. It contains other text fields that are either numbers or characters. However, all I want to do is to check the three text boxes that are supposed to have dates in them. I was wondering if you might have any suggestions as to how I might be able to accomplish this. Thanks again for your response. :)

I am using a function for the isDate(), but thanks for the suggestion. :)
 
First of all, update your script to the right javascript syntax - right now it's completely wrong.
Then, you should post the errors that you get, otherwise it's impossible to tell what's going on.
 
Hmm, it seems you are under the impression that javascript has a built in function called isDate like vbscript. It doesn't. you need to create it. either that or you didn't post it.
admin@onpntwebdesigns.com
 
I had already fixed my script to the correct javascript syntax. Here is the code that I am using:


<script language=&quot;JavaScript&quot;>
<!--
// Declaring valid date character, minimum year and maximum year
var dtCh= &quot;/&quot;;
var minYear=1900;
var maxYear=2100;

function isInteger(s){
var i;
for (i = 0; i < s.length; i++){
// Check that current character is number.
var c = s.charAt(i);
if (((c < &quot;0&quot;) || (c > &quot;9&quot;))) return false;
}
// All characters are numbers.
return true;
}

function stripCharsInBag(s, bag){
var i;
var returnString = &quot;&quot;;
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++){
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}

function daysInFebruary (year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
for (var i = 1; i <= n; i++) {
this = 31;
if (i==4 || i==6 || i==9 || i==11) {this = 30}
if (i==2) {this = 29}
}
return this;
}

function isDate(dtStr){
var daysInMonth = DaysArray(12);
var pos1=dtStr.indexOf(dtCh);
var pos2=dtStr.indexOf(dtCh,pos1+1);
var strMonth=dtStr.substring(0,pos1);
var strDay=dtStr.substring(pos1+1,pos2);
var strYear=dtStr.substring(pos2+1);
strYr=strYear;
if (strDay.charAt(0)==&quot;0&quot; && strDay.length>1) strDay=strDay.substring(1)
if (strMonth.charAt(0)==&quot;0&quot; && strMonth.length>1) strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)==&quot;0&quot; && strYr.length>1) strYr=strYr.substring(1)
}
month=parseInt(strMonth);
day=parseInt(strDay);
year=parseInt(strYr);
if (pos1==-1 || pos2==-1){
alert(&quot;The date format should be : mm/dd/yyyy&quot;);
return (false);
}
if (month<1 || month>12){
alert(&quot;Please enter a valid month&quot;);
return (false);
}
if (day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
alert(&quot;Please enter a valid day&quot;);
return (false);
}
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
alert(&quot;Please enter a valid 4 digit year between &quot;+minYear+&quot; and &quot;+maxYear);
return (false);
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
alert(&quot;Please enter a valid date&quot;);
return (false);
}
return true;
}


function checktheboxes() {
if (document.form1.regulator.value == &quot;&quot;)
{
alert(&quot;Please fill out the regulator field&quot;);
document.form1.regulator.focus();
return (false);
}
frm=document.form1

if(!isDate(frm.installdate.value) && frm.installdate.value != &quot;&quot;)
{
frm.installdate.focus();
return (false);
}
if(!isDate(frm.maintdate.value) && frm.maintdate.value != &quot;&quot;)
{
frm.maintdate.focus();
return (false);
}
if(!isDate(frm.readdate.value) && frm.readdate.value != &quot;&quot;)
{
frm.readdate.focus();
return (false);
}

return true;
}
-->
</script>

What happens is, if the first text box is blank and I enter in a date in the second, it tells me that the first text box needs a date in mm/dd/yy format because it is blank, then it checks the second one. What I want to be able to do is to check each individual box, not dependant on another. So, if the first box is empty, but the second one that I want to check has a value in it, I only want to check the second one, etc. I am using an isDate function. I tried to incorporate the coding that was posted in this thread. Thanks. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top