[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1676
  • Last Modified:

java script date validation for a textbox

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
santoshkumark
Asked:
santoshkumark
1 Solution
 
Michel PlungjanIT ExpertCommented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now