Hi here is my first post hope you can help me
I will try to explain the case without posting too much code
I am using a script which is connected to a paypal form
I have 2 radio buttons in the form and a text field
One radio button is for customers who do not want any engraved text, the other radio button is for those who want the text engraving.
I am stuck with the script becase I do want the script to force an entry in the text field if the user has selected the radio button for "no engraving text"
this is the part of the script where the entry is forced...

if ((obj.type == "text" ||        // just read text,
         obj.type == "textarea") &&
         obj.name != "tot" &&         //  but not from here
         obj.name != "quantity") {
      val = obj.value;                // get the data
      if (val == "" && tst) {         // force an entry
        alert ("Enter data for " + obj.name);
        return false;
      }
      StorVal ();

How do I change it so that only if the user selected the engraving text radio button, then the text input will be forced?

Member Avatar for langsor

I worked up a similar situation to what you are describing, hope you can find something useful in this to help you solve your problem ...

<html>
<head>
<script type="text/javascript">
window.onload = function () {
  document.getElementById('the_form').onsubmit = function () {
    for ( var i = 0; i < this.elements.engrave.length; i ++ ) {
      if ( this.elements.engrave[i].checked ) {
        var eng = this.elements.engrave[i].value;
      }
    }
    if ( eng && eng == 'yes' ) {
      var words = this.elements.engraving.value;
      if ( !words ) {
        alert( 'please enter an engraving text' );
        return false;
      } else {
        alert( words ); // do something with them here
      }
    }
  };
};
</script>
</head>
<body>
<form id="the_form">
  <label for="yes"><input id="yes" type="radio" name="engrave" value="yes" />Engrave</label>
  <label for="no"><input id="no" type="radio" name="engrave" value="no" />No Engrave</label>
  <input type="text" id="engraving" />
  <input type="submit" />
</form>
</body>
</html>

Cheers

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.