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
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    160
    Thanks
    40
    Thanked 0 Times in 0 Posts

    Conditional validation working only once

    Hi All,

    I am working on a script to enable or disable the field validation of some fields based on the selections made in some other fields. The function is called on the "selecting" field's onChange event. I can get this to work for one cyle (enable and disable the validation) but i cannot get this to work mor than once (if I try to re-enable the validation it will not work). I am sure it is because of the two variables I am declaring outside of the function but if I declare them within the function then the validation is not disabled at all. If I remove the "var" declaration I am getting an undefined error. I have been at this for way too long and need some help from the experts.

    Thanks in advance.
    Ken

    Code:
    <script type="text/javascript">
      var sprytextfield1;
      var sprytextfield2;
    
    function setValidation() {
      var list1=document.getElementById("List1").value;
      var list2=document.getElementById("List2").value;
      var theLocNumField=document.getElementById("Loc-Num");
      var theEquipNumField=document.getElementById("Equip-Num");  
        if (list1=="MOPS Station"||list1=="Finish Suite"||list1=="Edit Room"||list1=="Studio") {
          if (!sprytextfield1){
          sprytextfield1 = new Spry.Widget.ValidationTextField("spryLoc-Num", "none", {validateOn:["blur"],isRequired:true});
          }
        }else{
          if(sprytextfield1){
           sprytextfield1.resetClasses();
          sprytextfield1.destroy();
    Num", "none", {validateOn:["blur"],isRequired:false});
          delete sprytextfield1;
          theLocNumField.value="";
          }
        }
        if (list2=="VTR"||list2=="EVS"||list2=="KiPro"||list2=="Intercom"||list2=="Camera") {
          if(!sprytextfield2){
          sprytextfield2 = new Spry.Widget.ValidationTextField("spryEquip-Num", "none", {validateOn:["blur"],isRequired:true});
          }
        }else{
          if(sprytextfield2){
          sprytextfield2.resetClasses();
          sprytextfield2.destroy();
    Num", "none", {validateOn:["blur"],isRequired:false});
          delete sprytextfield2;    
          theEquipNumField.value="";
          }
        }  
    };
    </script>
    Last edited by VIPStephan; 07-11-2013 at 04:22 PM. Reason: fixed code BB tags

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    This line
    Code:
    Num", "none", {validateOn:["blur"],isRequired:false});
    and
    Code:
    Num", "none", {validateOn:["blur"],isRequired:false});
    Are wrong and keeping the function from working.
    Evolution - The non-random survival of random variants.

  • #3
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    160
    Thanks
    40
    Thanked 0 Times in 0 Posts
    Thanks for your reply sunfighter. I know that the line

    sprytextfield1 = new Spry.Widget.ValidationTextField("spryLoc-Num", "none", {validateOn:["blur"],isRequired:true});

    is what creates the validation. It is part of the spry framework that Adobe Dreamweaver uses. The script works fine on the first run and the validation can be created and destroyed once. But if the user then subsequently makes a selection in either List1 or List2 that should re-create the validation, it doesn't work.

    Can you help me understand what about those lines is wrong?

    Thanks,

    Ken

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    Don't you think we need to see the HTML calling this and affected by this. How else can we populate these variables
    var list1=document.getElementById("List1").value;
    var list2=document.getElementById("List2").value;
    Evolution - The non-random survival of random variants.

  • #5
    Regular Coder MaDmiX's Avatar
    Join Date
    Feb 2012
    Location
    Charlotte, NC
    Posts
    160
    Thanks
    40
    Thanked 0 Times in 0 Posts
    I got it figured out. For some reason it worked when I declared the variables sprytextfield1 and sprytextfield2 at the bottom of the page. Here is the working code:

    function setValidation() {

    var theList1 = document.getElementById("List1").value;
    var theList2 = document.getElementById("List2").value;
    var EquipLoc = document.getElementById("Loc-Num");
    var EquipID = document.getElementById("Equip-Num");

    if (theList1=="MOPS Station"||theList1=="Finish Suite"||theList1=="Edit Room"||theList1=="Studio") {

    // if there isn't a validaton, build one
    if(!sprytextfield1){
    sprytextfield1 = new Spry.Widget.ValidationTextField("spryLoc-Num", "none", {validateOn:["blur"]});
    }

    } else {

    // if there is a validaiton in sprytextfield destory it, and clear the variable
    if (sprytextfield1) {
    EquipLoc.value="";
    sprytextfield1.reset();
    sprytextfield1.destroy();
    sprytextfield1 = null;
    }
    }

    if (theList2=="VTR"||theList2=="EVS"||theList2=="KiPro"||theList2=="Intercom"||theList2=="Camera") {

    // if there isn't a validaton, build one
    if(!sprytextfield2){
    sprytextfield2 = new Spry.Widget.ValidationTextField("spryEquip-Num", "none", {validateOn:["blur"]});
    }

    } else {

    // if there is a validaiton in sprytextfield destory it, and clear the variable
    if (sprytextfield2) {
    EquipID.value="";
    sprytextfield2.reset();
    sprytextfield2.destroy();
    sprytextfield2 = null;
    }
    }
    // proceed with the rest as normal
    return true;
    };


    and the HTML that calls it:

    <tr>
    <td width="33%" height="69"><span id="spryLocation">
    <label for="List1">Location:</label><br />
    <select class="formField" name="List1" id="List1" tabindex="1" onchange="fillSelect(this.value,this.form['List2'])" onblur="setValidation()">
    <option selected></option>
    </select></br>
    <span class="selectRequiredMsg">Please select an item.</span></span>
    </td>

    <td width="33%"><span id="spryLoc-Num">
    <label for="Loc-Num">Location #:</label><br />
    <input type="text" class="formField" name="Loc-Num" id="Loc-Num" style="width:100px;" /></br>
    <span class="textfieldRequiredMsg">A value is required.</span></span>
    </td>
    <td width="33%">&nbsp;</td>

    </tr>
    <tr>
    <td><span id="spryEquipment">
    <label for="List2">Equipment:</label><br />
    <select name="List2" class="formField" id="List2" tabindex="2" onblur="setValidation()">
    </select></br>
    <span class="selectRequiredMsg">Please select an item.</span></span>
    </td>

    <td><span id="spryEquip-Num">
    <label for="Equip-Num">Equipment #:</label><br />
    <input type="text" class="formField" name="Equip-Num" id="Equip-Num" style="width:100px;" /></br>
    <span class="textfieldRequiredMsg">A value is required.</span></span>
    </td>
    <td>&nbsp;</td>
    </tr>


    and the bottom of the page:

    <script type="text/javascript">
    var sprytextfield1, sprytextfield2;
    var spryselect1 = new Spry.Widget.ValidationSelect("spryLocation", {validateOn:["blur"]});
    var spryselect2 = new Spry.Widget.ValidationSelect("spryEquipment", {validateOn:["blur"]});
    var sprytextarea1 = new Spry.Widget.ValidationTextarea("spryDescription", {validateOn:["blur"]});
    var spryselect3 = new Spry.Widget.ValidationSelect("sprySendTo", {validateOn:["blur"]});
    </script>


  •  

    Posting Permissions

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