SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2012
    Posts
    50
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    Making conditions in jquery validator

    JQuery Validate plugin is a good thing to control web-forms. I have learnt about it, but I want to make something extra.

    Suppose, I have a <select> tag with a few options. If a user selects a specific one, another input field gets added and it's required on that circumstance.

    But, I want to do, that field will only be required if the specific option is selected.

    For example:

    Code:
    <select id="any" name="any">
      <options>1</options>
      <options>2</options> <!-- make it specific -->
      <options>3</options>
    </select>
    <div id="hidden_f">
      <label for="any" id="any">Type</label>
      <input type="text" id="any" name="any">
    </div>
    If the user selects '2' in selection, the hidden field is shown and it's required. Otherwise, if the user didn't select '2', the field will not be shown and it's not required. How to accomplish such condition?

    I have read the JQUERY Validate Doc, but it's not so friendly for newbies. So I think you can explain me a lot.
    Thanks in advance....

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Ahmed Sadman View Post
    Suppose, I have a <select> tag with a few options. If a user selects a specific one, another input field gets added and it's required on that circumstance.

    But, I want to do, that field will only be required if the specific option is selected.
    When the form requirements change, you would need to remove the older validation rules and create a new set of validation rules that apply to the new form setup.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2012
    Posts
    50
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Ok. But how? I am new and can't understand. Again, my whole form requirements doesn't change, it only applies to one field.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Ahmed Sadman View Post
    Ok. But how? I am new and can't understand. Again, my whole form requirements doesn't change, it only applies to one field.
    It should be possible then to add and/or remove certain rules for the validation then. Let's see if that is possible with the validation plugin.

    And yes, it does indeed seem that there are methods to add and remove rules. If you go to the plugin methods section you will see them there. The add rules and remove rules methods.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2012
    Posts
    50
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Many many thanks. I didn't read that thing. But now it's clear


Bookmarks

Posting Permissions

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