String length validation in JavaScript is used to make sure that the number of characters entered in a specified field will be restricted. For example, the name field should have a length of 8-10 characters only.
Example:
<!DOCTYPE html> <html lang="en"> <head> <script> function stringLengthCheck(name, minlength, maxlength) { var mnlen = minlength; var mxlen = maxlength; if(name.value.length<mnlen || name.value.length> mxlen) { alert("Name should be " +mnlen+ " to " +mxlen+ " characters."); return false; } else { return true; } } </script> </head> <body> <div class="mail"> <h2>JavaScript String Length Validation</h2> <form name="form1" action="#"> Name: <input type='text' name='name'/></br></br> <input type="submit" name="submit" value="Submit" onclick="stringLengthCheck(document.form1.name, 8, 10)"/> </form> </div> </body> </html>