Validating input lenght using JavaScript

Juiniacap

New Member
I want to show an error if user enters less than 4 letters in text box.Here is the code I'm currently using now, it is somewhat working. The problem is - it is showing the error and then it is submitting the value also.I want to stop it submitting if user enters less than 4 letters.JavaScript\[code\]<script type="text/javascript">function check(what){ var length=what.search_text.value.length; if (length >3 && length < 21) what.submit(); else alert("Your nick should be 4-20 characters long.");}</script>\[/code\]HTML\[code\]<form method=post action=''><input type=hidden name=todo value=http://stackoverflow.com/questions/15566833/search><p align=center> Enter Your Name: <input type=text name=search_text value='$search_text'> <input onclick=check(this.form); return false; type=submit value=http://stackoverflow.com/questions/15566833/Search></p></form>\[/code\]
 
Back
Top