0

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;
}
4
Contributors
3
Replies
7
Views
7 Years
Discussion Span
Last Post by Taywin
0

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!

0

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]\[\]]/;
0

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!!!

Edited by Taywin: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.