javascript - Radio button and having a text box validation Ruby on Rails -


this setup have:

<input id="demographics_questionary_gender_male" name="demographics_questionary[gender]" type="radio" value="male"> male <input id="demographics_questionary_gender_gender" name="demographics_questionary[gender]" type="radio" value="female"> female <input id="demographics_questionary_gender_male" name="demographics_questionary[gender]" type="radio" value="other"> other <input id="other_gender_value" name="other_gender[value]" size="30" type="text"> 

this produce 3 radio buttons (male, female, other). there text box if "other" radio button selected.

my question deals how validate if text box filled in if "other" radio button selected.

currently, in controller check if other radio button selected , if i'll change stored value value set in "other" text box. however, don't know how validate text box. know how validate if radio button checked verifying value has been set in model, not how make sure user had inputed data "other" text box.

here snippet of code controller:

if @demographics_questionary.gender == 'other'     @other_gender = params[:other_gender]     if @other_gender[:value].nil?         @demographics_questionary.gender  = @other_gender[:value]     else          @demographics_questionary.gender  = 'not set'     end  end 

as can see, set 'not set' if 'other' radio button selected , 'other' text box empty. however, code executed when button selected process data. ideally, want validation done @ view page prevent user going forward until text box filled in if "other" radio button selected. in way, time hits controller method check not required.

but again i'm open other methods suggested.

i'm thinking can done javascript or jquery i'm not sure how.

oh yeah, code in view lies in form field:

<% form_for(@demographics_questionary) |f| %>   <%= f.error_messages :header_message => "please not skip of questions." %>   /* code radio buttons */   <%= f.submit 'next' %>      <%end%> 

notice radio button code located in form_for. once submit button triggered it'll execute method in controller. if use javascript/jquery how append function submit button?

this current page :

http://otoplusvn.com/therapistsurvey/demographics_questionaries/new question 2 on gender.

any suggestions? appreciate it, derek

you can add dependency rules, this:

$('#register_form').validate({rules: {      'other_gender[value]': {        required: {          depends: function(element) {            return $("input[name=demographics_questionary[gender]]:checked").val() == "other"          }        }      } } }); 

that require text box, only if selected option "other"


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -