Your code is not validated because you are attaching a submit
event to a text field. You must use forms for this.
In addition to the answer of the sir, which will work fine. Provide him with an alternative. You can also use regular expression checks.
A nice example that adds error messages after a text box.
$("#yourFormId").submit(function() { var inputVal= $("#yourTextBoxId").val(); var characterReg = /^([a-zA-Z0-9]{1,})$/; if(!characterReg.test(inputVal)) { $("#yourTextBoxId").after('<span class="error">Maximum 8 characters.</span>'); } });
source share