Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    CBG
    CBG is offline
    New Coder
    Join Date
    Feb 2010
    Location
    UK
    Posts
    91
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Question Validate radio button, and make sure text field is not blank

    Hi,

    On my SMF Forum, registration form, I am using custom fields, and want to make sure if the value="0" is dotted, it make sure teh text field is filled in, if not give an error when they click register.

    Code:
    <input type="radio" name="customfield[cust_platfo]" class="input_radio" id="customfield_cust_platfo_0" value="0" >
    <input type="radio" name="customfield[cust_platfo]" class="input_radio" id="customfield_cust_platfo_1" value="1" >
    <input type="radio" name="customfield[cust_platfo]" class="input_radio" id="customfield_cust_platfo_2" value="2" >
    Now if value="0" is dotted/selected I want the following text field to be completed

    Code:
    <input type="text" name="customfield[cust_playst]" maxlength="255" size="50" value="" class="input_text" />
    I am also going to need the full code, to give out a javascript error, when they click on the register button, as in what do I put in the form code etc...

    Thanks
    Last edited by CBG; 07-20-2012 at 09:50 AM.
    Regards,
    CBG

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,189
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    Is there some reason you used those oddball names, with square brackets?

    Also, is there some reason you gave each radio button an ID?

    Also, you say "I have a forum...". Surely you mean you have a form? As in HTML <form>?

    Anyway, when you don't show us the rest of the <form> or how you are doing the validation, it's hard to give you an answer. And we certainly can't answer your last question, about when "they click on the register button" when we don't know what your register button is (is it a submit button? a regular button? or what?) without the code.

    And, finally, you sure don't mean you want to "give out a javascript error", because if they aren't paying attention to their JavaScript error console [and 98% of people don't] they will never see the error. I would *assume* you want an error message *DISPLAYED*. But how do you want it displayed? You should *NOT* just use a JavaScript alert( ) as those can be disabled by users.

    So... More info, please, if you expect usable help.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,076
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    Based upon 'Old Pedant's comments, this is my guess ...
    Code:
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    // For: http://www.codingforums.com/showthread.php?t=268332
    
    function getRBtnName(GrpName) {
      var sel = document.getElementsByName(GrpName);
      var fnd = -1;
      var str = '';
      for (var i=0; i<sel.length; i++) {
        if (sel[i].checked == true) { str = sel[i].value;  fnd = i; }
      }
    //  return fnd;   // return option index of selection
    // comment out next line if option index used in line above  
      return str;
    } 
    function showFld(fldValue) {
      var styleDisplay = 'none';
      if (fldValue == '0') { styleDisplay = 'block'; }
      document.getElementById('Fld0').style.display = styleDisplay;
    }
    function validateForm() {
      var flag = true;
      if (getRBtnName('cust_platfo') == '0') {
        if (document.getElementById('fldInfo').value == '') { alert('Information needed'); flag = false; }
      }
      return flag;  
    // comment above and uncomment below for testing purposes only
    // alert('validate: '+flag);  return false;
    }
    </script>
    
    <style type="text/css">
    .hide { display:none; }
    </style>
    
    </head>
    <body>
    <form name="myForm" action="" method="post" onsubmit="return validateForm()">
     <input type="radio" name="cust_platfo" class="input_radio" onclick="showFld(this.value)" value="0" > X 
     <input type="radio" name="cust_platfo" class="input_radio" onclick="showFld(this.value)" value="1" > Y 
     <input type="radio" name="cust_platfo" class="input_radio" onclick="showFld(this.value)" value="2" > Z 
     <div id="Fld0" class="hide"> Information: 
      <input type="text" name="cust_playst" maxlength="255" size="50" value="" class="input_text" id="fldInfo" />
     </div>
     <p />
     <input type="submit" value="Submit" />
    </form>
    </body>
    </html>
    To avoid a lot of reloading, use the testing code instructions at the end of the "validateForm()" function.

  • #4
    CBG
    CBG is offline
    New Coder
    Join Date
    Feb 2010
    Location
    UK
    Posts
    91
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Ok, I have a form, on a SMF Forum, which is why the field names, are odd, as they are custom field made by the forum.

    I am not wanting to hide or show a field, I am wanting to make sure the text field is filled in
    Regards,
    CBG

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,076
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Exclamation

    Quote Originally Posted by CBG View Post
    Ok, I have a form, on a SMF Forum, which is why the field names, are odd, as they are custom field made by the forum.

    I am not wanting to hide or show a field, I am wanting to make sure the text field is filled in
    Remove the class='hide' and it should work for you.

    Only 'X' will check for the blank field as you asked.

    Concerning a SMF Forum problem, you might be able to ask them (???)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •