By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,483 Members | 1,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,483 IT Pros & Developers. It's quick & easy.

password field validation - help

P: n/a
I've been trying for the past week to put a simple code together. I
have done a LOT of searching, found scripts showing the functions I
would like to use, however when I mix them it all goes wrong, somehow
I always end up with error messages and functions not working right.
Can someone please help me?

I have a form, inside is 1 Text Field and 2 Password Fields. What I'm
looking to do is:
- Make sure password fields are equal
- Set Minimum/Maximum amount of characters in each field
- Disable submit button after clicking submit

I plan to add more text fields in the future. Will I be able to? I
know how to add it in the form, but how do I change the script?

Any help is appreciated. What I have so far may not be worth a look,
it's not even working, I don't know why, but here is goes:

<html>
<head>
<title>Test Page 01</title>
<script language="JavaScript">
<!--
function checkPw(element) {
pw1 = document.FormName.pw1.value;
pw2 = element.value;
if (pw1 != pw2) {
alert ("Passwords do not match. Please re-enter.");
document.FormName.pw1.value="";
document.FormName.pw2.value="";
document.FormName.pw1.focus();
return false;
}
}
// -->
</script>
</head>
<body>
<form action="http://www.photoshelf.net/cgi-bin/mailto"
onsubmit="return FrontPage_Form1_Validator(this)" method="post"
name="FormName">
<input type="hidden" name="THANKURL"
value="http://www.somepage.org/ok.htm">
<input type="hidden" name="RECIPIENT" value="so*****@somepage.com">
<input type="text" name="UserName" size="20" maxlength="60">
<input type="password" name="pw1" size="20" maxlength="60">
<input type="password" name="pw2" size="20" maxlength="60"
onBlur="checkPw(this)"><br>
<input type="submit" value="Submit Application" onClick="if(this.value
== 'Continue') this.form.submit();">
</form>
</body>
</html>

Thanks. Mike
Jul 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
ms*****@photoshelf.net (Mike) writes:
I have a form, inside is 1 Text Field and 2 Password Fields. What I'm
looking to do is:
- Make sure password fields are equal
- Set Minimum/Maximum amount of characters in each field
- Disable submit button after clicking submit

I plan to add more text fields in the future. Will I be able to?
Sure.
I know how to add it in the form, but how do I change the script?
It's modular. You add clauses for each thing you want to test.

Remember the <!DOCTYPE ...> declaration. It's required in HTML
<html>
<head>
<title>Test Page 01</title>
<script language="JavaScript">
The type attribute is required in HTML 4, and is always sufficient:
<script type="text/javascript">
<!--
Not necessary.
function checkPw(element) {
You call this function in the second password field's onblur handler.
Just wait until submission.

You only need one function to validate. We call it from the onsubmit
handler with the form as argument.
---
function validate(form) {
// check user anme between 6 and 10 characters:
var user = form.elements[UserName];
if (user.value.length < 6 || user.value.length > 10) {
alert("User name must be between 6 and 10 characters!");
user.focus();
return false;
}
// passwords equal and between 6 and 8 characters
var pwd1 = form.elements["pw1"];
var pwd2 = form.elements["pw2"];
if (pwd1.value != pwd2.value) {
alert("Passwords must be equal!");
pwd1.focus();
return false;
}
if (pwd1.length < 6 || pwd1.length > 8) {
alert("Passwords must be between 6 and 8 characters!");
pwd1.focus();
return false;
}
// form is ok, prevent further submit.
var submit = form.elements["submitButton"];
submit.value = "Already submitted!";
submit.disabled = true;
return true;
}
---
<form action="http://www.photoshelf.net/cgi-bin/mailto"
onsubmit="return FrontPage_Form1_Validator(this)" method="post"
name="FormName">
change onsubmit to
onsubmit="return validate(this);"
or whatever function name you want.
<input type="submit" value="Submit Application" onClick="if(this.value
== 'Continue') this.form.submit();">


Give the submit button a name so we can find and disable it.

Good luck
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
Mike wrote on 29 Nov 2003:
I've been trying for the past week to put a simple code
together. I have done a LOT of searching, found scripts showing
the functions I would like to use, however when I mix them it
all goes wrong, somehow I always end up with error messages and
functions not working right. Can someone please help me?

I have a form, inside is 1 Text Field and 2 Password Fields.
What I'm looking to do is:
- Make sure password fields are equal
- Set Minimum/Maximum amount of characters in each field
- Disable submit button after clicking submit
That's a dangerous thing to do. If a user clicks the 'Back' button,
the submit button will still be disabled (control states are usually
saved) and the user has to go back again, then forward to restore the
initial settings. It might not matter in this case, though (due to
the nature of the form).
I plan to add more text fields in the future. Will I be able to?
I know how to add it in the form, but how do I change the
script?
The text fields have nothing to do with the validation of the
password fields so you could add anything you like and it will have
no effect, or does the number of characters condition you state above
apply to literally /every/ field? If so, and the limits are the same
for all fields, or all types of field (60 for password types, 30 for
text, 1000 for textarea, etc), there is no problem. If not (the
limits vary from field to field) it could become a little more
complicated. You'll have to explain more about the changes you might
make.
<script language="JavaScript">
There is no need to use the language attribute (it is, in fact,
discouraged), but you /must/ use type (type="text/javascript")
<!--
You don't need to use SGML comments in SCRIPT blocks.
function checkPw(element) {
pw1 = document.FormName.pw1.value;
You should use the forms and elements collections, like so:

pw1 = document.forms['FormName'].elements['pw1'].value;

It aids in compatibility. You should also use the var keyword to
declare the variable, otherwise it ends up becoming a global.
pw2 = element.value;
if (pw1 != pw2) {
alert ("Passwords do not match. Please re-enter.");
You should qualify that using window. That is, window.alert(...). The
collections comment above applies to these, too:
document.FormName.pw1.value="";
document.FormName.pw2.value="";
document.FormName.pw1.focus();
return false;
Returning false should not be necessary here (and may not do
anything, anyway). The focus() call should keep the box in focus.
}
Not that it matters here, due to what I just wrote, but in future, I
would suggest that if you return a value for one code path, every
code path should return a value.
}
// -->
</script>
</head>
<body>
<form action="http://www.photoshelf.net/cgi-bin/mailto"
onsubmit="return FrontPage_Form1_Validator(this)" method="post"
name="FormName">
You're using intrinsic events, but you didn't (in this sample) set
the default scripting language. This is invalid HTML. Place the
following META element in the document HEAD:

<META http-equiv="Content-Script-Type" content="text/javascript">
<input type="hidden" name="THANKURL"
value="http://www.somepage.org/ok.htm">
<input type="hidden" name="RECIPIENT"
value="so*****@somepage.com">
This is a /huge/ security risk. Never use a mail form that requires a
full "To:" address - it's an easy target for spam (both for receiving
it, and sending it).
<input type="text" name="UserName"
size="20" maxlength="60"> <input type="password" name="pw1"
size="20" maxlength="60"> <input type="password" name="pw2"
size="20" maxlength="60" onBlur="checkPw(this)"><br>
The check should really be done upon submission. If I choose to do
something else before completing a matching password (including
changing my original password), I'll be trapped in this input field.
There's also no need to pass the entire object: just pass the value
using this.value.
<input type="submit" value="Submit Application"
onClick="if(this.value == 'Continue') this.form.submit();">


From what you've shown, that onclick event doesn't seem to do
anything. It would have been helpful if you had shown the function
called by the form's onsubmit event.

Apply the suggestions I've made so far. If it's not enough, say so,
and include the function called by the onsubmit event and explain
your restrictions in more detail, and I'll try again.

Good luck,
Mike

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #3

P: n/a
Michael Winter <M.******@blueyonder.co.uk.invalid> wrote in message news:<Xn*******************************@193.38.113 .46>...
Mike wrote on 29 Nov 2003:
I've been trying for the past week to put a simple code
together. I have done a LOT of searching, found scripts showing
the functions I would like to use, however when I mix them it
all goes wrong, somehow I always end up with error messages and
functions not working right. Can someone please help me?

I have a form, inside is 1 Text Field and 2 Password Fields.
What I'm looking to do is:
- Make sure password fields are equal
- Set Minimum/Maximum amount of characters in each field
- Disable submit button after clicking submit


That's a dangerous thing to do. If a user clicks the 'Back' button,
the submit button will still be disabled (control states are usually
saved) and the user has to go back again, then forward to restore the
initial settings. It might not matter in this case, though (due to
the nature of the form).
I plan to add more text fields in the future. Will I be able to?
I know how to add it in the form, but how do I change the
script?


The text fields have nothing to do with the validation of the
password fields so you could add anything you like and it will have
no effect, or does the number of characters condition you state above
apply to literally /every/ field? If so, and the limits are the same
for all fields, or all types of field (60 for password types, 30 for
text, 1000 for textarea, etc), there is no problem. If not (the
limits vary from field to field) it could become a little more
complicated. You'll have to explain more about the changes you might
make.
<script language="JavaScript">


There is no need to use the language attribute (it is, in fact,
discouraged), but you /must/ use type (type="text/javascript")
<!--


You don't need to use SGML comments in SCRIPT blocks.
function checkPw(element) {
pw1 = document.FormName.pw1.value;


You should use the forms and elements collections, like so:

pw1 = document.forms['FormName'].elements['pw1'].value;

It aids in compatibility. You should also use the var keyword to
declare the variable, otherwise it ends up becoming a global.
pw2 = element.value;
if (pw1 != pw2) {
alert ("Passwords do not match. Please re-enter.");


You should qualify that using window. That is, window.alert(...). The
collections comment above applies to these, too:
document.FormName.pw1.value="";
document.FormName.pw2.value="";
document.FormName.pw1.focus();
return false;


Returning false should not be necessary here (and may not do
anything, anyway). The focus() call should keep the box in focus.
}


Not that it matters here, due to what I just wrote, but in future, I
would suggest that if you return a value for one code path, every
code path should return a value.
}
// -->
</script>
</head>
<body>
<form action="http://www.photoshelf.net/cgi-bin/mailto"
onsubmit="return FrontPage_Form1_Validator(this)" method="post"
name="FormName">


You're using intrinsic events, but you didn't (in this sample) set
the default scripting language. This is invalid HTML. Place the
following META element in the document HEAD:

<META http-equiv="Content-Script-Type" content="text/javascript">
<input type="hidden" name="THANKURL"
value="http://www.somepage.org/ok.htm">
<input type="hidden" name="RECIPIENT"
value="so*****@somepage.com">


This is a /huge/ security risk. Never use a mail form that requires a
full "To:" address - it's an easy target for spam (both for receiving
it, and sending it).
<input type="text" name="UserName"
size="20" maxlength="60"> <input type="password" name="pw1"
size="20" maxlength="60"> <input type="password" name="pw2"
size="20" maxlength="60" onBlur="checkPw(this)"><br>


The check should really be done upon submission. If I choose to do
something else before completing a matching password (including
changing my original password), I'll be trapped in this input field.
There's also no need to pass the entire object: just pass the value
using this.value.
<input type="submit" value="Submit Application"
onClick="if(this.value == 'Continue') this.form.submit();">


From what you've shown, that onclick event doesn't seem to do
anything. It would have been helpful if you had shown the function
called by the form's onsubmit event.

Apply the suggestions I've made so far. If it's not enough, say so,
and include the function called by the onsubmit event and explain
your restrictions in more detail, and I'll try again.

Good luck,
Mike


Hi Mike: Without typing anything on the fields and simply clicking on
submit, IE6 errors:
"Line 22, Char 1, Object Expected"

I'm sure I screwed up!:

<html>
<head>
<title>Test Page 01</title>
<META http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function checkPw(element) {
pw1 = document.forms['FormName'].elements['pw1'].value;

pw2 = element.value;
if (pw1 != pw2) {
window.alert ("Passwords do not match. Please re-enter.");
document.FormName.pw1.value="";
window.alert ("Please enter a password.");
document.FormName.pw2.value="";
window.alert ("Please re-enter your password.");
document.FormName.pw1.focus();
}
// -->
</script>
</head>
<body>
<form action="http://www.photoshelf.net/cgi-bin/mailto"
onsubmit="return FrontPage_Form1_Validator(this)" method="post"
name="FormName">
<input type="text" name="UserName" size="20" maxlength="60">
<input type="password" name="pw1" size="20" maxlength="60">
<input type="password" name="pw2" size="20" maxlength="60"
"this.value"><br>
<input type="submit" value="Submit Application"
onSubmit="if(this.value == 'Continue') this.form.submit();">
</body>
</html>

Thanks in advance for all replies. Mike
Jul 20 '05 #4

P: n/a
Mike wrote on 30 Nov 2003:
<html>
<head>
<title>Test Page 01</title>
<META http-equiv="Content-Script-Type"
content="text/javascript"> <script type="text/javascript">
function checkPw(element) {
pw1 = document.forms['FormName'].elements['pw1'].value;

pw2 = element.value;
if (pw1 != pw2) {
window.alert ("Passwords do not match. Please
re-enter."); document.FormName.pw1.value="";
window.alert ("Please enter a password.");
document.FormName.pw2.value="";
window.alert ("Please re-enter your password.");
document.FormName.pw1.focus();
}
// -->
</script>
</head>
<body>
<form action="http://www.photoshelf.net/cgi-bin/mailto"
onsubmit="return FrontPage_Form1_Validator(this)" method="post"
name="FormName">
<input type="text" name="UserName" size="20" maxlength="60">
<input type="password" name="pw1" size="20" maxlength="60">
<input type="password" name="pw2" size="20" maxlength="60"
"this.value"><br>
<input type="submit" value="Submit Application"
onSubmit="if(this.value == 'Continue') this.form.submit();">
</body>
</html>


If this is exactly what you are testing, it's because the function
(used in the form onsubmit event) FrontPage_Form1_Validator() doesn't
exist.

You should remove the closing SGML comment (// -->) at the end of the
script block.

Finally, you should remove the onsubmit event on the submit button.
INPUT elements (of any type) don't have such an event, so it means
and does nothing.

Here's some advice to help you do the other things you asked about in
your original post:

You've already got the maximum lengths restricted in your HTML. To
set a minimum length, use [control reference].value.length. For
example:

document.forms['FormName'].elements['pw1'].value.length

To disable the submit button, place the following at the end of the
validation script so it will be executed when the form validates
correctly:

document.forms['FormName'].elements['submitButtonName'].disabled =
true;

If you make all these changes and add a proper onsubmit event handler
for the form, your document above would look something like:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<HTML>
<HEAD>
<META http-equiv="Content-Script-Type" content="text/javascript">

<TITLE>Test Page 01</TITLE>

<SCRIPT type="text/javascript">
function checkPw(form) {
var pw1 = form.elements['pw1'];
var pw2 = form.elements['pw2'];
var minPasswordLength = 6;

// Check password length
if ((minPasswordLength > pw1.value.length)
|| (minPasswordLength > pw2.value.length)) {
window.alert(
'Passwords must be at least ' + minPasswordLength
+ ' characters long.');
pw1.focus();
return false;
}

// Check password match
if (pw1.value != pw2.value) {
window.alert(
'The passwords do not match. Please re-enter.');
pw1.value = '';
pw2.value = '';
pw1.focus();
return false;
}

// By now, the passwords have been validated. The form can be
// submitted, and the submit button (I've named it 'apply'
// for this example) can be disabled.
form.elements['apply'].disabled = true;
return true;
}
</SCRIPT>
</HEAD>

<BODY>
<FORM name="FormName" method="post"
action="http://www.photoshelf.net/cgi-bin/mailto"
onsubmit="return checkPw(this)">
<INPUT type="text" name="UserName" size="20" maxlength="60">
<INPUT type="password" name="pw1" size="20" maxlength="60">
<INPUT type="password" name="pw2" size="20" maxlength="60">
<INPUT type="submit" name="apply" value="Submit Application">
</BODY>
</HTML>

Hope this helps,
Mike

Sorry, I didn't mean to write this for you, but there were quite a
few mistakes creeping in that would take too much explanation to fix.
I think I was giving you too much new information without explaining
it properly. My fault.

--
Michael Winter
M.******@blueyonder.co.uk.invalid (remove ".invalid" to reply)
Jul 20 '05 #5

P: n/a
Michael Winter <M.******@blueyonder.co.uk.invalid> wrote in message news:<Xn*******************************@193.38.113 .46>...
Mike wrote on 30 Nov 2003:
<html>
<head>
<title>Test Page 01</title>
<META http-equiv="Content-Script-Type"
content="text/javascript"> <script type="text/javascript">
function checkPw(element) {
pw1 = document.forms['FormName'].elements['pw1'].value;

pw2 = element.value;
if (pw1 != pw2) {
window.alert ("Passwords do not match. Please
re-enter."); document.FormName.pw1.value="";
window.alert ("Please enter a password.");
document.FormName.pw2.value="";
window.alert ("Please re-enter your password.");
document.FormName.pw1.focus();
}
// -->
</script>
</head>
<body>
<form action="http://www.photoshelf.net/cgi-bin/mailto"
onsubmit="return FrontPage_Form1_Validator(this)" method="post"
name="FormName">
<input type="text" name="UserName" size="20" maxlength="60">
<input type="password" name="pw1" size="20" maxlength="60">
<input type="password" name="pw2" size="20" maxlength="60"
"this.value"><br>
<input type="submit" value="Submit Application"
onSubmit="if(this.value == 'Continue') this.form.submit();">
</body>
</html>


If this is exactly what you are testing, it's because the function
(used in the form onsubmit event) FrontPage_Form1_Validator() doesn't
exist.

You should remove the closing SGML comment (// -->) at the end of the
script block.

Finally, you should remove the onsubmit event on the submit button.
INPUT elements (of any type) don't have such an event, so it means
and does nothing.

Here's some advice to help you do the other things you asked about in
your original post:

You've already got the maximum lengths restricted in your HTML. To
set a minimum length, use [control reference].value.length. For
example:

document.forms['FormName'].elements['pw1'].value.length

To disable the submit button, place the following at the end of the
validation script so it will be executed when the form validates
correctly:

document.forms['FormName'].elements['submitButtonName'].disabled =
true;

If you make all these changes and add a proper onsubmit event handler
for the form, your document above would look something like:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<HTML>
<HEAD>
<META http-equiv="Content-Script-Type" content="text/javascript">

<TITLE>Test Page 01</TITLE>

<SCRIPT type="text/javascript">
function checkPw(form) {
var pw1 = form.elements['pw1'];
var pw2 = form.elements['pw2'];
var minPasswordLength = 6;

// Check password length
if ((minPasswordLength > pw1.value.length)
|| (minPasswordLength > pw2.value.length)) {
window.alert(
'Passwords must be at least ' + minPasswordLength
+ ' characters long.');
pw1.focus();
return false;
}

// Check password match
if (pw1.value != pw2.value) {
window.alert(
'The passwords do not match. Please re-enter.');
pw1.value = '';
pw2.value = '';
pw1.focus();
return false;
}

// By now, the passwords have been validated. The form can be
// submitted, and the submit button (I've named it 'apply'
// for this example) can be disabled.
form.elements['apply'].disabled = true;
return true;
}
</SCRIPT>
</HEAD>

<BODY>
<FORM name="FormName" method="post"
action="http://www.photoshelf.net/cgi-bin/mailto"
onsubmit="return checkPw(this)">
<INPUT type="text" name="UserName" size="20" maxlength="60">
<INPUT type="password" name="pw1" size="20" maxlength="60">
<INPUT type="password" name="pw2" size="20" maxlength="60">
<INPUT type="submit" name="apply" value="Submit Application">
</BODY>
</HTML>

Hope this helps,
Mike

Sorry, I didn't mean to write this for you, but there were quite a
few mistakes creeping in that would take too much explanation to fix.
I think I was giving you too much new information without explaining
it properly. My fault.


Excellent, worked beautiful, I can take it from here! Thanks a lot,
you saved me a lot of time. Thanks.
Mike
Jul 20 '05 #6

P: n/a
ms*****@photoshelf.net (Mike) wrote in message news:<5e*************************@posting.google.c om>...
Michael Winter <M.******@blueyonder.co.uk.invalid> wrote in message news:<Xn*******************************@193.38.113 .46>...
Mike wrote on 30 Nov 2003:
<html>
<head>
<title>Test Page 01</title>
<META http-equiv="Content-Script-Type"
content="text/javascript"> <script type="text/javascript">
function checkPw(element) {
pw1 = document.forms['FormName'].elements['pw1'].value;

pw2 = element.value;
if (pw1 != pw2) {
window.alert ("Passwords do not match. Please
re-enter."); document.FormName.pw1.value="";
window.alert ("Please enter a password.");
document.FormName.pw2.value="";
window.alert ("Please re-enter your password.");
document.FormName.pw1.focus();
}
// -->
</script>
</head>
<body>
<form action="http://www.photoshelf.net/cgi-bin/mailto"
onsubmit="return FrontPage_Form1_Validator(this)" method="post"
name="FormName">
<input type="text" name="UserName" size="20" maxlength="60">
<input type="password" name="pw1" size="20" maxlength="60">
<input type="password" name="pw2" size="20" maxlength="60"
"this.value"><br>
<input type="submit" value="Submit Application"
onSubmit="if(this.value == 'Continue') this.form.submit();">
</body>
</html>


If this is exactly what you are testing, it's because the function
(used in the form onsubmit event) FrontPage_Form1_Validator() doesn't
exist.

You should remove the closing SGML comment (// -->) at the end of the
script block.

Finally, you should remove the onsubmit event on the submit button.
INPUT elements (of any type) don't have such an event, so it means
and does nothing.

Here's some advice to help you do the other things you asked about in
your original post:

You've already got the maximum lengths restricted in your HTML. To
set a minimum length, use [control reference].value.length. For
example:

document.forms['FormName'].elements['pw1'].value.length

To disable the submit button, place the following at the end of the
validation script so it will be executed when the form validates
correctly:

document.forms['FormName'].elements['submitButtonName'].disabled =
true;

If you make all these changes and add a proper onsubmit event handler
for the form, your document above would look something like:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<HTML>
<HEAD>
<META http-equiv="Content-Script-Type" content="text/javascript">

<TITLE>Test Page 01</TITLE>

<SCRIPT type="text/javascript">
function checkPw(form) {
var pw1 = form.elements['pw1'];
var pw2 = form.elements['pw2'];
var minPasswordLength = 6;

// Check password length
if ((minPasswordLength > pw1.value.length)
|| (minPasswordLength > pw2.value.length)) {
window.alert(
'Passwords must be at least ' + minPasswordLength
+ ' characters long.');
pw1.focus();
return false;
}

// Check password match
if (pw1.value != pw2.value) {
window.alert(
'The passwords do not match. Please re-enter.');
pw1.value = '';
pw2.value = '';
pw1.focus();
return false;
}

// By now, the passwords have been validated. The form can be
// submitted, and the submit button (I've named it 'apply'
// for this example) can be disabled.
form.elements['apply'].disabled = true;
return true;
}
</SCRIPT>
</HEAD>

<BODY>
<FORM name="FormName" method="post"
action="http://www.photoshelf.net/cgi-bin/mailto"
onsubmit="return checkPw(this)">
<INPUT type="text" name="UserName" size="20" maxlength="60">
<INPUT type="password" name="pw1" size="20" maxlength="60">
<INPUT type="password" name="pw2" size="20" maxlength="60">
<INPUT type="submit" name="apply" value="Submit Application">
</BODY>
</HTML>

Hope this helps,
Mike

Sorry, I didn't mean to write this for you, but there were quite a
few mistakes creeping in that would take too much explanation to fix.
I think I was giving you too much new information without explaining
it properly. My fault.


Excellent, worked beautiful, I can take it from here! Thanks a lot,
you saved me a lot of time. Thanks.
Mike


One last thing, to control minimum length on text field, I should add
the line below to the script right?

document.forms['FormName'].elements['UserName'].value.length

Where in the script? Does location matter?
Thanks again. Mike
Jul 20 '05 #7

P: n/a
Mike wrote on 06 Dec 2003:

<snipped script>
One last thing, to control minimum length on text field, I
should add the line below to the script right?

document.forms['FormName'].elements['UserName'].value.length

Where in the script? Does location matter?


The minimum length is already checked by the script I wrote:

function checkPw(form) {
var pw1 = form.elements['pw1'];
var pw2 = form.elements['pw2'];
var minPasswordLength = 6;

// Check password length
if ((minPasswordLength > pw1.value.length)
|| (minPasswordLength > pw2.value.length)) {
window.alert(
'Passwords must be at least ' + minPasswordLength
+ ' characters long.');
pw1.focus();
return false;
}

// Omitted the rest of the script...

If you want to change the minimum length, change the initial value of
the minPasswordLength variable. If you want to impose minimum limits
on other fields, copy that if statement block and alter it as needed.
For example:

// To check username length
var username = form.elements['UserName'];
var minUsernameLength = 4;

if (minUsernameLength > username.value.length) {
window.alert(
'Your username must be at least ' + minUsernameLength
+ ' characters long.');
username.focus();
return false;
}

Hope that helps,
Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk")
Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.