I am making a name picker and I want to detect if your user has input a name or if they are just messing with the website. Is there a way to do this? Code:
<html>
<textarea id="names"></textarea>
<input type="submit" value="Pick" onclick="onClick(this)">
<p id="chosen"></p>
</html>
<script>
function onClick(x) {
//Your code
}
</script>
CodePudding user response:
The getNames() method is called for the <input> element to check for value when it submits, and a value is returned after the value inside the <textarea> element is checked.
/* This helper method checks whether the login has been made or not. */
function isNameEmpty(){
var name = document.getElementById("names");
if(name.value.length != 0){
console.log(name.value);
return false;
}
else{
console.log('No value was entered.');
return true;
}
}
/* This submit method is used to return the entered name. */
function getNames(){
if(!isNameEmpty()){
return document.getElementById("names").value;
}
else{
alert('No value was entered.');
return '';
}
}
<textarea id="names"></textarea>
<input type="submit" value="Pick" onclick="return getNames();">
<p id="chosen"></p>
References
CodePudding user response:
you can change the textarea to input tag and you can use browser built in attributes like type= text min max length etc.
otherwise you should write a function that reads and evaluates the input value of the textarea
