We help IT Professionals succeed at work.

Javascript validation for Date/ Numeric  value gives the alert but Submits the form- it shouldn't submit

puneetfred
puneetfred asked
on
The problem is that the alert works okay but the form is submitted, since the function return false, i would like to understand why it is happening and how do i make submit to not happen, if the date is not valid.

The code for validation which checks whether the field has a date in mm-dd-yyyy. It then has the following function which checks for this validation:

<script language="Javascript">function checkForm() {
 if (!checkDate(document.forms[0].PurchaseDate.value))
     {
     alert ("Please enter a valid date in format- dd/mm/yyyy.");
          return false;
     }
  else
     return true;
  }</script>  
---------------------------X------------------

This is called from the form through the following:
[<INPUT TYPE=submit onClick="checkForm()" VALUE="Submit Your Ad">] [<input type="reset" value="Start Over"></FORM>]
 [<!--button//-->] '>> This is to hide the default button.

     
Comment
Watch Question

Commented:
Hello,

insert a return statement before function name in onClick event definition, like this:
<INPUT TYPE=submit onClick="return checkForm()" VALUE="Submit Your Ad">

If this does not work for you, than the next stage is not to declare the button with type=submit, but simply type=button and call the form.submit() from the check function.

But try first the return prefix.

Regards,
zvonko

Author

Commented:
first one worked!! Thanks a lot!!!!

Commented:
hey, how fast do you read this? it's incredible <|;-)

Thank you for the points and for the feedback.

Regards,
zvonko

Explore More ContentExplore courses, solutions, and other research materials related to this topic.