I'm trying to validate some fields in my form but can't get it to work. I think it's because of the submit button code. Could someone please help identify the problem?

<script language="JavaScript">
function validate_form ( )
{
if ( document.leaveform.dc1.value == "" )
        {
                alert ( "Please select start date" );
                valid = false;
                return valid;
 
       }
}    


<input name="b1" type="button" value="Submit Without Preview"
ONCLICK ="this.form.action ='fastsaveregistrationdetails.jsp';this.form.submit()">

try putting it in the form's onsubmit event, like this:

<form onsubmit="return validate_form()">

Hi,

try this Code its working

code given by BudBrocken is right :)

</head>
<script language="JavaScript">
function validate_form ( )
{
if ( document.leaveform.dc1.value == "" )
{
alert ( "Please select start date" );
valid = false;
return valid;
//document.leaveform.submit();
}
}
</script>


<body>

<form action="fastsaveregistrationdetails.jsp" method="post" name="leaveform" onsubmit="return validate_form()">
<input name="dc1" type="text" />
<input name="b1" type="submit" value="Submit Without Preview">
</form>
</body>

Best regards,
Rahul

The problem is i cant change the following code as i'm using it for having multiple submit buttons on one form. Please advice on how i can run the validation script based on this. Thank you..

<input name="b1" type="button" value="Submit Without Preview"
ONCLICK ="this.form.action ='fastsaveregistrationdetails.jsp';this.form.submit()">
Member Avatar for GreenDay2001

You could use if decision with the values returned by the validate_form().

Actually I am not getting your Question, but try this code

<script language="JavaScript">
function validate_form ( )
{

if ( document.leaveform.dc1.value == "" )
{
alert ( "Please select start date" );
valid = false;
return valid;
document.leaveform.submit();
}
}
</script>
<body>
<form  method="post" name="leaveform" >
  <input name="dc1" type="text" />
    <input name="b1" type="submit" value="Submit Without Preview" onclick="this.form.action ='fastsaveregistrationdetails.jsp';return validate_form()">

</form>
</body>

feel free to ask or clearify the quetion

Regards,
Rahul

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.