Link to home
Start Free TrialLog in
Avatar of Massimo Scola
Massimo ScolaFlag for Switzerland

asked on

JavaScript: Issue with onClick

I'm trying to validate a textbox if it is a date before submitting a form via php.

For testing purposes, I created a simple html page with a button and an onClick() event which should call the function.
The return value is a boolean (which I have hardcoded for testing.).

The date validation is not complete yet; I need to check if the regular expression works and if the value/date is a leap year and the February date. Or is the issue the expression check?

The problem is that the onClick event doesn't work. Can anyone spot the error?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
<script type="application/javascript" language="javascript">

	formIsOK = false;

	function checkDate() {
		
	var theExpression = /^[0-9]{4}\-(0[1-9]¦1[012])\-(0[1-9]|[12][0-9]3[01])/;
	var getDate = document.getElementBy("theDate");
	var matchData = getDate.value.match(theExpression);

	var theYear = m[1];
	var theMonth = m[2] - 1;
	var theDay = m[3];

	var newDate = new Date(theYear,theMonth,theDay);
	alert(newDate);
	formIsOK = true;
	}
</script>
<body>

<p>Date validation: </p>

<table width="200" border="1">
  <tbody>
    <tr>
      <td>Date</td>
      <td><label for="textfield">Text Field:</label>
      <input type="text" name="date" id="theDate" /></td>
    </tr>
    <tr>
      <td></td>
      <td><input type="button" value="Click me" onClick="checkDate()"></td>
    </tr>
  </tbody>
</table>
</body>
</html>

Open in new window

SOLUTION
Avatar of Mukesh Yadav
Mukesh Yadav
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Massimo Scola

ASKER

So I tried this code.. I forgot that m is an array.

I changed the alert() function to alert(getDate). It still doesn't work.
So where is the bug which I can't see?

<script type="application/javascript" language="javascript">

	formIsOK = false;

	function checkDate() {
		
	var theExpression = /^[0-9]{4}\-(0[1-9]¦1[012])\-(0[1-9]|[12][0-9]3[01])/;
	var getDate = document.getElementById("theDate");
	var matchData = getDate.value.match(theExpression);

	
	var theYear;
	var theMonth;
	var theDay;
	
	var m[matchData];
		
	theYear = m[1];
	theMonth = m[2] - 1;
	theDay = m[3];
		
	var newDate = new Date(theYear,theMonth,theDay);
		
	alert(newDate);
	
	formIsOK = true;
	}
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Now the function works .. but I have an issue with the regular expression which I'm going to ask as a new question. Thanks for your help!