Hi everyone, i am trying to validate password entered into my system from a jsp page, i want to create a method that would check that a password entered is not less than six characters and not more than 15. Does anyone have an idea on how i could go about this. Cheers.:sad:

Dani AI

Generated

A concise, correct approach is to validate on the server (JSP/Servlet) and use client-side checks only for user experience. The thread contains useful attempts but a few practical mistakes worth calling out: used a condition that excludes the boundary values (6 and 15); had duplicate element IDs and an invalid JS NULL check; moved to type="password" which is the right direction. Server-side validation must always be authoritative.

A minimal, correct server-side helper in Java:

private static boolean isPasswordLengthValid(String pw) {
    return pw != null && pw.length() >= 6 && pw.length() <= 15;
}

Example use in a servlet POST handler:

String pw = request.getParameter("password");
if (!isPasswordLengthValid(pw)) {
    request.setAttribute("error", "Password must be 6 to 15 characters.");
    request.getRequestDispatcher("/register.jsp").forward(request, response);
    return;
}

Client-side (for immediate feedback) — ensure unique IDs and type="password":

function validatePwd() {
  var p = document.getElementById('Npass').value;
  var c = document.getElementById('Cpass').value;
  if (p.length < 6 || p.length > 15) { alert('Password must be 6–15 chars'); return false; }
  if (p !== c) { alert('Passwords do not match'); return false; }
  return true;
}

Practical notes and checklist:

  • Never rely solely on client-side checks; always re-validate on the server.
  • Do not auto-trim passwords unless the application explicitly wants to disallow leading/trailing spaces.
  • Prefer stronger policies in modern apps (longer minimums, strength checks) even if the requirement here is 6–15.
  • Store passwords hashed with a current, adaptive algorithm (bcrypt/argon2) and never in plaintext.
  • For Java frameworks, consider bean validation (@Size(min=6,max=15)) to centralize rules.

This addresses the original length requirement while fixing the common pitfalls seen earlier in the thread.

Recommended Answers

All 6 Replies

String pass = "crap";

If ( pass.length() > 6 && pass.length() < 15 )
{
}

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
function validatePwd()
{
	var pw1 = document.getElementById("Npass").value;
	var pw2 = document.getElementById("Cpass").value;
	// check for a value in both fields.
if (pw1 ==NULL  || pw2 == NULL) 
	{
	alert('Please enter your password .');
	return false;
	}

if (pw1 != pw2)
	{
		alert ("You did not enter the same new password twice. Please re-enter your password.");
		return false;
	}
else 
		{
			alert('Nice job.');
			return true;
      	}
}
</script>
</head>
<body>
<form name="form1" method="post" action="" >

<p>New Password: 
  <input name="Npass" type="text" id="Npass">
</p>
<p>Confirm Password: 
  <input name="Cpass" type="text" id="Npass">
</p>
<p>
  <input type="submit" name="Submit" value="Submit" onClick="return validatePwd()">
</p>
</form>
</body>
</html>

@ Nikhil: Wouldn't it be better to check the date before posting. the question was asked almost 5 years ago.

and to check the technology?
considering the fact that this is a 'Java' forum, giving a solution in javascript might not be what the OP was looking for.

now ... next to mine and verruckt24's remark, there was another issue with his post, anyone found that one yet? :)
(Yeah, I'm bored ..)

<html>
<head>
<title>Pwd validation using js</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
function validatePwd()

{


var pw1 = document.getElementById("Npass").value;
var pw2 = document.getElementById("Cpass").value;

if (pw1 ==null || pw2 ==null || pw1 == '' || pw2 =='' )
{  alert('The Pwd field is balank or user not confirmed the pwd');
    document.form1.Npass.value=''
    document.form1.Cpass.value=''
return false;	}

if (pw1!= pw2)	{
alert ("You did not enter the same new password twice. Please re-enter your password.");
document.form1.Npass.value=''
document.form1.Cpass.value=''


return false;
}else
{alert('Nice job.');
return true;
}
}
</script>
</head>
<body><form name="form1" method="post" action="" >
<p>New Password:   <input name="Npass" type="password" id="Npass"></p>
<p>Confirm Password:<input name="Cpass" type="password" id="Cpass">
</p><p>  <input type="submit" name="Submit" value="Submit" onClick="return validatePwd()">
</p>
</form>
</body>
</html>
commented: Pleae learn difference between Java and JavaScript, also please read previous replies before dropping your code without any comment -3

Abdul ...
in the two posts previous to your own, we've given two reasons why NOT to revive or answer to this thread.
not obvious enough?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.