0
votes

Good evening,

I'm trying to pass an HTML input (a string) value as a JavaScript Function Parameter. I am pretty new to coding and recently started DOM Manipulation.


          function lengthOfPassword (strLength) {
  
            if (strLength.length >= 5) {
              return true; 
            } else {
              return false; 
            };  
            }
            console.log(lengthOfPassword("l"));

This is the original function I wrote, and it performs as needed returning true or false depending on the length.

I have used the onclick="()" and document.getElementById("").value; to take the value from the HTML input and have it run in the function.

This is how far I have got:

<body>
    <label for="password">Password:</label>

    <input id="pwd" type="text" minlength="5" />
    <button id="btn" onclick="lengthOfPassword()" type="button">Submit</button>

    <script src="DOM.js"></script>
  </body>

and my JS function now looks like this:

        
        function lengthOfPassword() {

        let strlength = document.getElementById("pwd").value;
  
          if (strlength.length >= 5) {
            return true; 
          } else {
            return false; 
          };  
          }

I feel like I am tripping at the finish line! If someone could help explain where I have gone wrong, that would be great!

1
What are you trying to do with the true/false that is returned from the function? - miklando
You should never use unsafe inline on* attributes. Use rather Element.addEventListener() right from your JS file or tag. - Roko C. Buljan
Your last version should work. But it's not very useful, since the return value of the function isn't used for anything. - Barmar
Thank you, Roko C. Buljan - I'll try the Element.addEventListener(). Oh, thank you, Barmar. What I am trying to do is if someone enters less than a 5 character string, it will output false (I plan on adding an alert) so if the string is too short, the user will know with a message like "not valid" - Joe

1 Answers

0
votes
  • Create a function isValidPassword(pwd, min) that handles the password string length and returns a boolean value
  • Use addEventListener() to assign a "click" event to your Submit button

/** Utility functions: */

/**
 * Get Element by selector (in parent Element)
 * @param {string} sel Selector
 * @param {object} el Parent Element (default = document)
 * @return {Element|null} Element
 */
const EL = (sel, el) => (el||document).querySelector(sel);

/**
 * Validate String length
 * @param {string} pwd Password
 * @param {number} min Min password length (default = 5)
 * @return {boolean} true if password matches min length
 */
const isValidPassword = (pwd, min = 5) => pwd.length >= min;

/** App */

const EL_pwd = EL("#pwd");
const EL_btn = EL("#btn");

const handleSubmit = () => {
  const isValid =  isValidPassword(EL_pwd.value);
  if (isValid) {
    console.log("Valid!");
  } else {
    console.log("Password is not valid");
  }
};

EL_btn.addEventListener("click", handleSubmit);
<label for="password">Password:</label>

<input id="pwd" type="text" minlength="5" />
<button id="btn" type="button">Submit</button>