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@website.com" /><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>

3 Answers3

1

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

var x = document.getElementsByName("os0")[0].value;
Kaf
  • 33,101
  • 7
  • 58
  • 78
1

hope this too myt help :

var x=document.myForm.os0.value;
Amit Singh
  • 2,267
  • 4
  • 25
  • 50
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

cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • i wasnt familiar with the jquery plugin. I tried it in the head tags but still to no avail. – user2859888 Oct 08 '13 at 19:15
  • It's not automagic... You need to study the docs and then code your document to use it. – cssyphus Oct 08 '13 at 19:18
  • Also see [this SO post](http://stackoverflow.com/questions/19259078/genvalidator-check-for-checkbox-in-form-validation/19283603#19283603) re form validation – cssyphus Oct 09 '13 at 23:02