Просто введите return следующим образом:
function validate()
{
var username=document.getElementById('username').value;
if(username=="")
{
return false;
}
if(username.length<5&&username.length<15)
{
alert("Length should between 5 to 15 characters");
return false;
}
return true;
}
body{
padding: 0px;
margin:0px;
}
input{
display: block;
margin-bottom:10px;
height:25px;
width:200px;
}
input[type="submit"]{
margin:auto;
}
label{
display:block;
margin-bottom:10px;
}
form{
width:500px;
height:500px;
border:1px solid green;
padding:10px;
margin:auto;
top:0;
bottom:0;
left:0;
right:0;
position:absolute;
}
.labels{
width: 35%;
box-sizing:border-box;
display: inline-block;
}
.inputs{
width: 63%;
box-sizing:border-box;
padding:10px;
display: inline-block;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>FORM VALIDATION</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<form action="#">
<div class="labels">
<label>Name</label>
<label>Email</label>
</div>
<div class="inputs">
<input type="text" name="username" id="username" required>
<input type="text" name="email" id="email" required>
</div>
<input type="submit" value="submit" onclick="return validate();">
</form>
</body>
</html>
Вернуть true и false из validate (); функция согласно требованию