Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody. I'm attempting to add a required password with a min. of 4 characters long. I've added the code for the password, but do not quite understand how to add the minimum amount of characters to this. Here is the code so far.

In advance, I appreciate the help.
HTML
<!--Begin: Login.html -->

<HTML>
	<HEAD>
		<STYLE>
		.defaultText { font-family: Helvetica,Arial; font-size:10pt; }
		</STYLE>

		<SCRIPT LANGUAGE="JAVASCRIPT">
		    function validate()
		    {
			if (document.forms.main.username.value == "")
		        {
			    alert("Username cannot be empty.");
			    return false;
			}
			else if (document.forms.main.password.value == "")
			{
			    alert("Password cannot be empty.");
			    return false;
			}	
			return true;
		     }
		</SCRIPT>
		<SCRIPT>
			Function validate()
			{
			if (document.forms.main.password.value >4);
				{
				alert ("Password must be at least 4 characters long.");
				return false
				}
				return true;
			}
		
		</SCRIPT>
		
	</HEAD>
	
	<BODY>
		<FORM NAME="main" ACTION="ShowAllItems.html" METHOD="POST">
			<table>


				<tr class="defaultText">
				    	<td>username:</td>
					<td><INPUT TYPE="text" NAME="username"></INPUT></td>			
				</tr>


	
				<tr class="defaultText">
				   	<td>password:</td>
					<td><INPUT TYPE="password" NAME="password"></INPUT></td>
				</tr>
		


				<tr>
				    <td colspan="2" align="center">
				    <INPUT TYPE="submit" VALUE="Submit"

				 önclick="return validate()"></INPUT>
				     </td>
				</tr>
			</table>	</FORM>
	
</HTML>
Posted
Updated 26-Oct-12 10:21am
v2

1 solution

Change if (document.forms.main.password.value >4);
to if (document.forms.main.password.value.length < 4);

A second issue I see, however, is that you have two functions listed as validate().
You should either rename one or move all the code into a single function. My suggestion would be to use a single validate function.
 
Share this answer
 
v3
Comments
Donato Hernandez 26-Oct-12 17:12pm    
//If changing the line to this
if (document.forms.main.password.value.length < 4);

does would I have to create a separate change it in the table too?

for example. I currently have

<TR CLASS="defaultText">
<TD>password:</TD>
<TD><INPUT TYPE="password" NAME="password"></INPUT></TD>
</TR>

which is in order to add my initial password. Would I create another one, just adding
length<4 </Input><TD>
fjdiewornncalwe 26-Oct-12 17:15pm    
No it should work fine as it is.
Sergey Alexandrovich Kryukov 26-Oct-12 17:24pm    
Good catch, a 5, but could be other problems...
--SA
fjdiewornncalwe 27-Oct-12 13:23pm    
Agreed that there are many other potential gotcha's here, but clearly the OP is quite new and I thought he could learn something valuable from even this small instruction.
Sergey Alexandrovich Kryukov 27-Oct-12 22:53pm    
Right...
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900