JavaScript string length validation

String length validation in JavaScript is used to make sure that number of characters entered in a specified field will be restricted. For example name field should have a length of 8-10 characters only.


<!DOCTYPE html>
<html lang="en">
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;
return true;
<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(, 8, 10)"/>
Content Protection by
Please Share