1

This is for help with a website I am building for class
assignment so far

Trying to make a function to compare input text to match certain words that I choose. So for this example if the user inputs anything other than bark or Bark in the text area it should just tell you in console it is incorrect. I plan on using this for a bigger function that passes a true/false that validates the name, date,phone, etc... but now I'm just trying to get this simple code to run in jsfiddle without a "this is not a function" in the console.

HTML

<form name="inputForm" onsubmit="verify() " enctype="text/plain">
  <input id="verify" type="text" name=verify placeholder="Dog talk and tree clothes?" required="required"/>
  <input type="submit" name="submit" value="submit" />
</form>

Javascript

var val = document.getElementById('verify').value;
function verify(val){
if(val === 'bark' || val === 'Bark'){
  console.log("yes");
} else {
  console.log("no");
}}
AuxTaco
  • 4,883
  • 1
  • 12
  • 27
Chris
  • 65
  • 1
  • 6

2 Answers2

1

function validateForm(e) {
  const inputValue = document.getElementById('verify').value;

  const wordsToVerify = ['Bark', 'word2', 'word3', 'etc'];

  // Loop through words array to convert values to Lower case.
  // Please note that you may not need to convert both words and value
  // Use .includes() method to check if input value is included in words array

  const wordFound = wordsToVerify.map(word => word.toLowerCase()).includes(inputValue.toLowerCase());

  if (wordFound) {
    console.log('yes');
    // Do something...
  } else {
    console.log('no');
  }
  // Prevent form submitting until you decide what to do the values
  return false;
}
<form onsubmit="return validateForm()">

  <input id="verify" type="text" name=verify placeholder="Dog talk and tree clothes?" required="required" />

  <button type="submit">submit!</button>

</form>

Check this post for more scenarios.

awran5
  • 4,333
  • 2
  • 15
  • 32
0

Cehck out the following snippet . In this we have called the function verify() on button click instead of input="submit"

function verify(){

  var val = document.getElementById('verify').value;



  if(val.toLowerCase() === 'bark' ){

   console.log("yes");

  } else {

   console.log("no");

  }
 }
 
<!DOCTYPE html>
<html lang="en">
<head>
 
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>GnG</title>
</head>
<body>
 <form name="inputForm"  enctype="text/plain">

  <input id="verify" type="text" name="verify" placeholder="Dog talk and tree clothes?" required="required"/>
 </form>
 <button onClick="verify()">
  Submit
 </button>

</body>
</html>
If you have any doubt , feel free to ask .
Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43