Akshay Sharma
Akshay Sharma

Reputation: 13

Validate text field while typing

Without any form and submit button. if entered character is less than 3. show error next to text field.

Upvotes: 1

Views: 1609

Answers (1)

Tobiq
Tobiq

Reputation: 2657

You can use a div to show error like so:

    var textbox = document.getElementById("textbox-id");
        divAfterTextBox = document.getElementById("divAfterTextBox-id");
    
    textbox.onkeyup = function(){
        if (textbox.value.length < 3) divAfterTextBox.textContent = "ERROR";
        else divAfterTextBox.textContent = "";
        // divAfterTextBox.textContent = textbox.value.length < 3 ?
        //    "ERROR" : divAfterTextBox.textContent = "";
    }
<input id="textbox-id" type="text">
<div id="divAfterTextBox-id"></div>

Upvotes: 1

Related Questions