I have a simple html form and i have a small piece of script which validates it,
I would like it so that after the validation is carried out, if the return value is true, then the user will be redirected to index.html
i have tried several pieces of code to the end of my validation script but i cant get the page to change
Any help would be appreciated

Here is my javascript code

function validate_form ( )
{
   var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;

   valid = true;

   if ( document.contactform.firstname.value == "" )
        {
                document.contactform.firstname.style.background= 'Yellow';
                alert ( "Please fill in the 'First Name' box." );
                valid = false;
        }

   if ( document.contactform.lastname.value == 0 )
        {
                document.contactform.lastname.style.background= 'Yellow';
                alert ( "Please fill in the 'Last Name' box." );
                valid = false;
        }        

   if (document.contactform.email.value.match(illegalChars)) 
        {

                document.contactform.email.style.background= 'Yellow';
                alert ( "Illegal Characters" );
                valid = false;
        }
  
   if ( document.contactform.email.value == 0 )
        {
                document.contactform.email.style.background= 'Yellow';
                alert ( "Please fill in the 'Email' box." );
                valid = false;
        }        

   if ( document.contactform.message.value == 0 )
        {
                document.contactform.message.style.background= 'Yellow';
                alert ( "Please fill in the 'Message' box." );
                valid = false;
        }        
      
   return valid;
}

Your javascript seems good but do you have something like this in your html form?

<form action="submit.html" onsubmit="validate_form()">...</form>

If your function returns true then the form will be submitted and the user redirected to submit.html. Else (if the function returns false) the user will stay on the page and the error message will pop.

Hope that works!

Your illegalchars VAR contains unescaped speechmarks, is this cancelling out some of your code as it's enclosed in unexpected speech marks?

var illegalChars= /[\(\)\<\>\,\;\:\\\/\[B]"[/B]\[\]]/;

I think it should be...

<form action="submit.html" onsubmit="return validate_form()">...</form>

instead of...

<form action="submit.html" onsubmit="validate_form()">...</form>

EDIT: This is old thread!!!

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.