none
Error on no value RRS feed

  • Question

  • Hello,

    I have the below code as a small function in my HTA. 

    function DButton()  { 
    		
    		 var array = ['A','B','C','D','a','b','c','d'];
    		 var str = "A,B,C,D,a,b,c,d";
    		 var x = document.getElementById("myText").value
    		 var pos = str.lastIndexOf(x);
    		 if (x !== -1 || x !== "" || x !== undefined || pos !== 15 || x !== null ) {
    		 Check.disabled = true
    		 document.getElementById("text").style.display = "block"
    		 document.getElementById("explain").style.display = "block"
    		 document.getElementById("submit").style.display = "block"
    		 document.getElementById("text").focus();
    		 }
    		 else{
    		alert("This is not a valid answer please select again.");
    		}
    		
    		 }

    Basically what happens at the moment is is a user just clicks on submit everything carries on as normal. As you can see I am trying to stop this from happening but I am unsure what I need to change to get it to know that the user has not entered entering into the myText element. 

    Could someone help point me in the right direction? 

    Thanks

    James

    Tuesday, December 10, 2019 11:56 AM

All replies

  • Use "!=" not "!=="

    "Submit" does not work in an HAT the way you expect.


    \_(ツ)_/

    Tuesday, December 10, 2019 5:55 PM
  • Hello,

    Thanks for the response. 

    I have tried to the changes you have suggested and I still have the same issue unfortunately. 

    Thanks

    James

    Wednesday, December 11, 2019 8:48 AM
  • When you test for ("X not equal a" OR "X not equal b"), that will always be true no matter what value is in X. 

    function DButton()  { 
    		 var array = ['A','B','C','D','a','b','c','d'];
    		 var str = "A,B,C,D,a,b,c,d";
    		 var x = document.getElementById("myText").value
    		 var pos = str.lastIndexOf(x);
    		 if (pos == -1 || x == "" || typeof(x) == "undefined" || pos == 15 || x == null ) {
    				alert("This is not a valid answer please select again.");
    		}	 
    		 else{
    			 Check.disabled = true
    			 document.getElementById("text").style.display = "block"
    			 document.getElementById("explain").style.display = "block"
    			 document.getElementById("submit").style.display = "block"
    			 document.getElementById("text").focus();
    		}
    }

     

    Wednesday, December 11, 2019 2:58 PM
  • Ahh thank you its good to have someone else look over code as I was too focused on doing it the way I was I never thought about flipping it on its head! 

    Thanks

    Thursday, December 12, 2019 11:22 AM
  • Also it is a better method to always use positive logic and not negative logic. This prevents confusion and makes coding and understanding code easier.


    \_(ツ)_/

    Thursday, December 12, 2019 11:26 AM