Hi I had a form validation script with me. that is

<head>
<script type="text/javascript">
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false;}
else {return true}
}
}
function validate_form(contact)
{
with (contact)
{
if (validate_required(username,"Userame must be filled out!")==false)
  {username.focus();return false;}
}
}
</script>
</head>

<body>
<form action="password_process.php" method="post" name="contact" onsubmit="return validate_form(this)">
</body>

This works fine for me. Now I need to check whether the user must enter minimum 6 letters in the box. Please help me how to do that or any changes in the same script is possible.......

Please help me....
Thanks in Advance

Recommended Answers

All 2 Replies

Now I need to check whether the user must enter minimum 6 letters in the box.

Use length .

if(form.username.length < 6) {
 alert('Username less than 6 characters..');
}

Btw, I don't understand your code. What will with (contact) { will do ?

Or you can also use RegExp to get precise handling with your form's.
Here's a simple demo:

<html>
<head>
<title>Form field validation demo</title>
<script type="text/javascript">
<!--
/* Field must contain atleast 6 characters long and must start with an alphabetic character(s) */
 
var userName = /^[A-Za-z\d]{6,12}$/;

/* same with the first pattern, but must be writen all in lowercase (you may include digit's). */
var passWord = /^[a-z\d]{6,12}$/;

function validate_form( form ) {
var un = form.username.value;
var pw = form.passwrd.value;
var alertxt = [];
if (!userName.test(un)) {
alertxt[alertxt.length] = 'Please provide a valid, username!'; }

if (!passWord.test(pw)) { alertxt[alertxt.length] = 'Invalid password!'; }

if (alertxt.length > 0) {
alertUser(alertxt); return false; } return true;
}

function alertUser(alertxt) {
var message = 'You have provided an invalid information in the following field(s):\n';

for (var x = 0; x < alertxt.length; x++) {
var invalidData = ((x)+1);
message += '\n' + invalidData + '. ' + alertxt[x]; }
alert(message); 
}
//-->
</script>
</head>
<body>
<form id="contact" action="process_password.php" method="post" onsubmit="return validate_form(this);">
<div>
<label>username: <input type="text" name="username" id="username" value="" /></label><br />
<label>password: <input type="password" name="passwrd" id="passwrd" value="" /></label><br />
<input type="submit" name="btn" id="btn" value="Submit" /></div>
</form>
</body>
</html>
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.