why does this function submit the form in Internet Explorer

Hi,

I have  a function that checks for special characters first in a text area and if there are some present then redirects to an error page and if  none are there then submits the form.

It works in chrome but in I.E. if there are special characters it will redirect tot the error page AND submit the form... multiple times.

Here is what i have so far

Thanks for the help

<script language="JavaScript" type="text/javascript">
			function submitPage(){  
			var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
            for (var i = 0; i < document.contactForm.txt_Message.value.length; i++) {
                 if (iChars.indexOf(document.contactForm.txt_Message.value.charAt(i)) != -1) {
                  window.location.replace("error.html"); 
				  return false ();
	}
	else
	 {
				var SS= Spry.Widget.Form.validate(document.contactForm);		
				document.forms["contactForm"].hdn_formaction.value="Submitted";
				document.forms["contactForm"].action="<!--$HttpCgiPath-->";
				if(SS==true){
				document.contactForm.submit();
				}
				//document.forms["contactForm"].submit()
				}
}				}
			</script>



   <form id="contactForm" name="contactForm" method="post" encType="multipart/form-data" />
<textarea class="notSpecilChar"  id="txt_Message" name="txt_Message" rows="4" wrap="virtual" cols="42" maxlength="400"></textarea>
<p><br/><a class="button" style="width:100px; padding-bottom:40px;" href="javascript:submitPage()"><span>Submit</span></a><br/></p>
 <script type="text/javascript">
  			var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "custom", {regExpFilter:/^[a-z0-9 ]*$/i, useCharacterMasking:true, validateOn:["blur"]});
			var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "email", {validateOn:["blur"]});
			var sprytextarea1 = new Spry.Widget.ValidationTextarea("sprytextarea1", {validateOn:["blur"],isRequired:true});
		</script>
 </form>

Open in new window

smfmetro10Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
You have the else clause within the for loop. There shouldn't be an else clause.  What you currently have checks on character at a time from iChars agains the textarea value.  Let's say the textarea value is "Hi" (without the quotes).
You first compare "!" against "Hi".  The if condition fails, so the else clause then submits the form asynchronously -- meaning it doesn't wait for the submit() to complete before iterating again.  Then the loop goes to the second iChar - namely "@".  The if fails again, and the else clause is again executed, submitting again.

Copy and paste the code below and try again:
<script type="text/javascript">
function submitPage()
{
    var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
    for (var i = 0; i < document.contactForm.txt_Message.value.length; i++)
    {
        if (iChars.indexOf(document.contactForm.txt_Message.value.charAt(i)) != -1)
        {
            window.location.replace("error.html");
            return false;
        }
    }

    /*
    // I did not test this, but I believe it will work
    if( /[!@#$%^&\*\(\)\+=\[\]\\\';,.\/\{\}\|":<>\?-]/.test(document.contactForm.txt_Message.value) )
    {
        window.location.replace("error.html");
    return false;
    }
    */

    var SS= Spry.Widget.Form.validate(document.contactForm);
    document.forms["contactForm"].hdn_formaction.value="Submitted";
    document.forms["contactForm"].action="<!--$HttpCgiPath-->";
    if(SS==true)
    {
        document.contactForm.submit();
    }
return false;
}
</script>



<form id="contactForm" name="contactForm" method="post" encType="multipart/form-data">
    <textarea class="notSpecilChar"  id="txt_Message" name="txt_Message" rows="4" wrap="virtual" cols="42" maxlength="400"></textarea>
    <p><br/><a class="button" style="width:100px; padding-bottom:40px;" href="#" onclick="return submitPage()"><span>Submit</span></a><br/></p>
</form>
 <script type="text/javascript">
            var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "custom", {regExpFilter:/^[a-z0-9 ]*$/i, useCharacterMasking:true, validateOn:["blur"]});
            var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "email", {validateOn:["blur"]});
            var sprytextarea1 = new Spry.Widget.ValidationTextarea("sprytextarea1", {validateOn:["blur"],isRequired:true});
</script>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
smfmetro10Author Commented:
Thank you so much! Out of curiosity why do you need the second window.location.replace("error.html")  ?

It never seems to get called.

Thanks again!!
0
hieloCommented:
>>  why do you need the second window.location.replace("error.html")  ?
You don't!  Notice that it is wrapped in /*  ... */ which instructs the javascript interpreter to ignore everything in between.  I left that as a comment for you.  You can either use lines 4-12 or lines 15-20 to achieve your goal.  I just chose to leave 4-12 since that was closer to what you had originally.
0
smfmetro10Author Commented:
Ah! Got it!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.