This script is supposed to show an alert if the text is longer than eight characters, what is wrong with it?

<!DOCTYPE html>
<html>
<body>

<input id="input"><button onclick="myFunction()">Click me</button>


<script>
function myFunction()
{
var x=document.getElementById("input").value
if (x = > 8)
        {
        alert("Text must be lower than 8 letters")
        return false;
        }
}
</script>

</body>
</html>

please help, thanks

Recommended Answers

All 2 Replies

you should count the character of the input first

var x =  document.getElementById("input").value;
var len = x.length;
if(len>=8){
     alert("Text must be lower than 8 letters")
    return false;
}

you can try this too:

var x=document.getElementById("input").value.length;
if (x  >= 8)
        {
        alert("Text must be lower than 8 letters")
        return false;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.