Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date Value

Posted on 2003-11-20
9
Medium Priority
?
268 Views
Last Modified: 2010-04-09
I have several input forms that have dates on them.  I want to make sure that people input the dates properly mm/dd/yy or day month year...

Is there a routine that can do this?
0
Comment
Question by:pinkstonm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 9792505
You are going to have problems with dates like 10/10/03 which could be either American or European format however here is a strart for the number of digits an d's /'s

<script>
function test(D){
if (/^\d{2}\/\d{2}\/\d{2}$/.test(D))
return true
else
return false
}
alert(test("22/1/12"))
alert(test("22/10/12"))
</script>

0
 

Author Comment

by:pinkstonm
ID: 9792588
Whats the html code to invoke it?
0
 

Author Comment

by:pinkstonm
ID: 9793420
       <INPUT NAME="ced" onmouseover="fillHelp('The expiration / renewal date of the current program contract. Input data in the following format: Day-Month-Year; (i.e. –Mar-2003)')" onmouseout="fillHelp(' ')" SIZE=15 style="font-family: Arial; font-size: 10px; color: #000080" dir="ltr">

How do I connect the script to the INPUT statement
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:jaysolomon
ID: 9793725
<html>
<head>
<script type="text/javascript">
<!--
function validate(fObj)
{
      var err = "";
      var focusField = "";
      var objRegExp = /^\d{2}\/\d{2}\/\d{2}$/; // mm/dd/yy
      if(!objRegExp.test(fObj["ced"].value)){
            err += "- CED";
            focusField = "ced";
      }
      if(err != ""){
            alert("The following fields contain errors:\n\n"+ err);
                  fObj[focusField].focus();
                        return false;
            }
      return true;
}
// -->
</script>
<style type="text/css">
<!--
.ced{
      font-family: Arial;
      font-size: 10px;
      color: #000080;
}
-->
</style>
</head>
<body>
<form name="form1" onsubmit="return validate(this);">
      <INPUT NAME="ced" class="ced"  SIZE=15 dir="ltr">
      <input type="submit" name="btnSubmit" value="Submit">
</form>
</body>
</html>
0
 

Author Comment

by:pinkstonm
ID: 9796196
Is there not a way to do date validation either onfocus or onclick?
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9798674
<INPUT NAME="ced" class="ced"  SIZE=15 dir="ltr" onchange="return validate(this.form);">
0
 

Author Comment

by:pinkstonm
ID: 9799335
I have seens screens on the internet that when you begin typing in them the force __/__/__
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9799361
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--
function formatDate(strField){
    var oLen = strField.value.length;
     if(oLen == 2 || oLen == 5){strField.value += "/";}
}
// -->
</script>
</head>
<body>
<form name="form1" method="post" action="">
Date <input type="text" name="Date" maxlength="10" size="11" onkeypress="formatDate(this);" />
</form>
</body>
</html>
0
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 200 total points
ID: 9801937
Put the script in the head

<script>
function test(D){
if (/^\d{2}\/\d{2}\/\d{2}$/.test(D))
 return true
else
 {alert("please input date in DD/MM/YY form")
 return false
 }
}
</script>

<form action="http://google.com">
Date<input type="text" name="date">
<input type="submit" value="submit" onClick="if (test(date.value)) submit();return false;">
</form>
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question