Solved

java script date validation for a textbox

Posted on 2008-06-19
2
1,660 Views
Last Modified: 2013-11-06
I have a text box and I have to enter a date and I have to validate it
if  date is correct then i will press a button
a confirmation box should come when that button is pressed and this confirmation box should come only after textbox validations

i.e.
1. textbox should not be empty
2. it should contain valid date

 and if the textbox is empty I am displaying a message (labelerror) that it should not be empty

and if the confirmation box comes that label error should not come

i.e.
I will enter an invalid date and press button then labelerror will show a error message
then i will enter a valid date  then confirmation box should come and the label error should be cleared because as confirmation box is javascript the confirmation box will come and the label error is not cleared as it is server side  i.e. I want to clear the label error in the java script itself
please assist me

 
0
Comment
Question by:santoshkumark
2 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 21828806
You mean

(if you want a really good date library go here http://www.javascripttoolbox.com/lib/date/source.php)
<script>
 
function isDate(year, month, day) {
  month -= 1; // js months start at 0
  var d = new Date(year,month,day);
  retur  (year == d.getFullYear() && month == d.getMonth() && day == d.getDate()) 
}
 
function validate(theForm) {
  document.getElementById('labelError').display='none';
  var date = theForm.date.value;
  // assuming dd/mm/yyyy
  var dParts = date.split('/');
  if (!isDate(dParts[2],dParts[1],dParts[0]) {
    document.getElementById('labelError').display='';
    return false
  }
  return confirm('Are you sure')
}
</script>
<form onSubmit="return validate(this)">
<input type="text" name="date" value="" /><span id="labelError" style="display:none">Invalid date</span>
</form>

Open in new window

0

Featured Post

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Auto-indent certain lines in Notepad++ 10 145
Scan Network and Export Reg value to single file 2 50
Find unused columns in a table 12 75
Powershell PGP and SFTP 1 18
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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)

726 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