2

I'm using some javascript to validate a form. It works fine in Firefox, IE10 and Chrome. Is there a way to make this below code work in Safari and IE9? When the donate button is clicked, it should be required to enter a student's name. But in Safari and IE9 the mandatory field is not being recognized, it just takes you right to paypal.

 <script>
function validateForm()
{
var x=document.forms["myForm"]["os0"].value;
if (x==null || x=="")
  {
  alert("Please Enter the Child Your Sponsoring and Click Donate");
  return false;
  }
}
</script>
<form name="myForm" action="https://www.paypal.com/cgi-bin/webscr" onsubmit="return  validateForm()" method="post">

<input type="hidden" name="on0" value="Child Sponsored Name" />Please enter the student's First and Last Name Your Sponsoring and click Donate

<input type="text" maxlength="200" name="os0" required/><input type="hidden" name="cmd" value="_donations" />

<input type="hidden" name="business" value="[email protected]" /><input type="hidden" name="lc" value="US" />

<input type="hidden" name="item_name" value="5k Run Fundraiser" />

<input type="hidden" name="no_note" value="0" />

<input type="hidden" name="cn" value="ADD DONOR NAME" />

<input type="hidden" name="no_shipping" value="2" />

<input type="hidden" name="currency_code" value="USD" />

<input type="hidden" name="bn" value="PP-DonationsBF:btn_donateCC_LG.gif:NonHosted" />

<input type="image" alt="PayPal - The safer, easier way to pay online!" name="submit" src="https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif" />

<img alt="" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1" border="0" />

</form>
6
  • 1
    I'm sure there is a way, but you haven't told us at all what the actual problem is to begin with. Why not use modern DOM methods, like getElementById? Commented Oct 8, 2013 at 18:47
  • break down the form to minimum fields find the culprit fields. Commented Oct 8, 2013 at 18:48
  • Yes IE is reporting the following error. Message: 'document.forms.myForm.os0.value' is null or not an object Line: 108 Char: 1 Code: 0 Commented Oct 8, 2013 at 18:48
  • var x=document.forms["myForm"]["os0"].value is an obsolete way of querying for DOM nodes. Commented Oct 8, 2013 at 18:49
  • It works for me in Safari 5.1.10. Commented Oct 8, 2013 at 18:50

3 Answers 3

1

Try this (not sure why you need form element):

var x = document.getElementsByName("os0")[0].value;
Sign up to request clarification or add additional context in comments.

4 Comments

i tried that code and IE throws this error: Webpage error details Message: 'document.getElementById(...)' is null or not an object Code: 0 But it still works for firefox.
Why does your error reference getElementById when he put getElementsByname in his answer?
@CollinGrady, I had it before and then corrected. He may have tried before correcting.
thanks this solution worked Kaf! I forgot to refresh my page.
1

hope this too myt help :

var x=document.myForm.os0.value;

Comments

0

This doesn't answer what your problem is, but it may get you around the problem.

Are you familiar with this jQuery plugin:

http://jqueryvalidation.org/documentation/

To use it, you will need to load both the jQuery library and the jQueryValidation plugin in the head tags of your document:

<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
</head>

See this demo

3 Comments

i wasnt familiar with the jquery plugin. I tried it in the head tags but still to no avail.
It's not automagic... You need to study the docs and then code your document to use it.
Also see this SO post re form validation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.