📄 password field validation - help - javascript.htm
字号:
<SCRIPT language=JavaScript
src="password field validation - help - JavaScript_files/show_ads.js"></SCRIPT>
</DIV>
<DIV
style="FONT-SIZE: 11pt; LINE-HEIGHT: 140%; MARGIN: 10px 0px 0px; PADDING-BOTTOM: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 0px"><!-- question: post the questions -->I've
been trying for the past week to put a simple code together.
I<BR>have done a LOT of searching, found scripts showing the
functions I<BR>would like to use, however when I mix them it all
goes wrong, somehow<BR>I always end up with error messages and
functions not working right.<BR>Can someone please help me?<BR><BR>I
have a form, inside is 1 Text Field and 2 Password Fields. What
I'm<BR>looking to do is:<BR>- Make sure password fields are
equal<BR>- Set Minimum/Maximum amount of characters in each
field<BR>- Disable submit button after clicking submit<BR><BR>I plan
to add more text fields in the future. Will I be able to? I<BR>know
how to add it in the form, but how do I change the
script?<BR><BR>Any help is appreciated. What I have so far may not
be worth a look,<BR>it's not even working, I don't know why, but
here is
goes:<BR><BR><html><BR><head><BR><title>Test Page
01</title><BR><script
language="JavaScript"><BR><!--<BR>function checkPw(element)
{<BR>pw1 = document.FormName.pw1.value;<BR>pw2 =
element.value;<BR>if (pw1 != pw2) {<BR>alert ("Passwords do not
match. Please
re-enter.");<BR>document.FormName.pw1.value="";<BR>document.FormName.pw2.value="";<BR>document.FormName.pw1.focus();<BR>return
false;<BR>}<BR>}<BR>//
--><BR></script><BR></head><BR><body><BR><form
action="http://www.photoshelf.net/cgi-bin/mailto"<BR>onsubmit="return
FrontPage_Form1_Validator(this)"
method="post"<BR>name="FormName"><BR><input type="hidden"
name="THANKURL"<BR>value="http://www.somepage.org/ok.htm"><BR><input
type="hidden" name="RECIPIENT"
value="someone@somepage.com"><BR><input type="text"
name="UserName" size="20" maxlength="60"><BR><input
type="password" name="pw1" size="20" maxlength="60"><BR><input
type="password" name="pw2" size="20"
maxlength="60"<BR>onBlur="checkPw(this)"><br><BR><input
type="submit" value="Submit Application"
onClick="if(this.value<BR>== 'Continue')
this.form.submit();"><BR></form><BR></body><BR></html><BR><BR>Thanks.
Mike<BR><!-- attachements information --></DIV><!-- subscribe, unsubscribe, quote -->
<DIV style="FONT-SIZE: 9pt; MARGIN: 15px 0px 10px"><A
href="http://bytes.com/forum/newreply.php?do=newreply&noquote=1&p=311139"
rel=nofollow><IMG alt="post reply" border=0
src="password field validation - help - JavaScript_files/answer_question.jpg"></A>
<BR><A
href="http://bytes.com/forum/newreply.php?do=newreply&noquote=0&p=311139"
rel=nofollow style="FONT-WEIGHT: bold">Quote</A> | <A
href="http://bytes.com/forum/subscription.php?do=addsubscription&t=92680"
rel=nofollow style="FONT-WEIGHT: bold">Subscribe</A> </DIV><!-- edited by -->
<DIV style="FONT-SIZE: 9pt; MARGIN: 20px 0px 10px"><!-- quick links for members and mods -->
<DIV style="TEXT-DECORATION: underline"><!-- controls --><!-- / controls --></DIV></DIV></DIV>
<DIV style="MARGIN: 0px">
<SCRIPT language=JavaScript
src="password field validation - help - JavaScript_files/as_header.js"></SCRIPT>
<SCRIPT language=JavaScript
src="password field validation - help - JavaScript_files/show_ads.js"></SCRIPT>
</DIV>
<DIV
style="FONT-SIZE: 13pt; FONT-WEIGHT: bold; PADDING-BOTTOM: 5px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 15px">7
Answers Posted </DIV><!-- / post #311141 --><!-- post #311141 -->
<DIV
style="BORDER-TOP: #999 1px solid; MARGIN-BOTTOM: 15px; MARGIN-TOP: 5px; PADDING-BOTTOM: 50px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 20px"><!-- user info -->
<DIV><!-- avatar float left --><A
href="http://bytes.com/forum/member.php?u=" rel=nofollow><IMG
alt="Lasse Reichstein Nielsen's Avatar" border=0 class=avtr
src="password field validation - help - JavaScript_files/noavatar.gif"></A>
<DIV id=postmenu_311141>
<DIV style="FONT-SIZE: 9pt">Lasse Reichstein Nielsen <SPAN
style="FONT-WEIGHT: normal"><A
href="http://bytes.com/forum/post311141-2.html" rel=nofollow
style="FONT-SIZE: 9pt" target=new>July 20th, 2005 01:25 PM</A>
</SPAN></DIV>
<DIV style="COLOR: #666; FONT-SIZE: 9pt">Guest - n/a Posts </DIV>
<DIV style="COLOR: #666; FONT-SIZE: 9pt">#2: Re: password field
validation - help</DIV></DIV></DIV><!-- / user info --><BR>
<DIV style="FONT-SIZE: 10pt; MARGIN-LEFT: 64px"><!-- message, attachments, sig --><!-- message -->
<DIV style="PADDING-TOP: 10px"><A
href="http://bytes.com/forum/register.php" rel=nofollow>Join
Bytes!</A> (Mike) writes:<BR>[color=blue]<BR>> I have a form,
inside is 1 Text Field and 2 Password Fields. What I'm<BR>>
looking to do is:<BR>> - Make sure password fields are
equal<BR>> - Set Minimum/Maximum amount of characters in each
field<BR>> - Disable submit button after clicking
submit<BR>><BR>> I plan to add more text fields in the future.
Will I be able to?[/color]<BR><BR>Sure.<BR>[color=blue]<BR>> I
know how to add it in the form, but how do I change the
script?[/color]<BR><BR>It's modular. You add clauses for each thing
you want to test.<BR><BR>Remember the <!DOCTYPE ...>
declaration. It's required in HTML<BR>[color=blue]<BR>>
<html><BR>> <head><BR>> <title>Test Page
01</title><BR>> <script
language="JavaScript">[/color]<BR><BR>The type attribute is
required in HTML 4, and is always sufficient:<BR><script
type="text/javascript"><BR>[color=blue]<BR>>
<!--[/color]<BR><BR>Not necessary.<BR>[color=blue]<BR>>
function checkPw(element) {[/color]<BR><BR>You call this function in
the second password field's onblur handler.<BR>Just wait until
submission.<BR><BR>You only need one function to validate. We call
it from the onsubmit<BR>handler with the form as
argument.<BR>---<BR>function validate(form) {<BR>// check user anme
between 6 and 10 characters:<BR>var user =
form.elements[UserName];<BR>if (user.value.length < 6 ||
user.value.length > 10) {<BR>alert("User name must be between 6
and 10 characters!");<BR>user.focus();<BR>return false;<BR>}<BR>//
passwords equal and between 6 and 8 characters<BR>var pwd1 =
form.elements["pw1"];<BR>var pwd2 = form.elements["pw2"];<BR>if
(pwd1.value != pwd2.value) {<BR>alert("Passwords must be
equal!");<BR>pwd1.focus();<BR>return false;<BR>}<BR>if (pwd1.length
< 6 || pwd1.length > 8) {<BR>alert("Passwords must be between
6 and 8 characters!");<BR>pwd1.focus();<BR>return false;<BR>}<BR>//
form is ok, prevent further submit.<BR>var submit =
form.elements["submitButton"];<BR>submit.value = "Already
submitted!";<BR>submit.disabled = true;<BR>return
true;<BR>}<BR>---<BR>[color=blue]<BR>> <form
action="http://www.photoshelf.net/cgi-bin/mailto"<BR>>
onsubmit="return FrontPage_Form1_Validator(this)"
method="post"<BR>> name="FormName">[/color]<BR><BR>change
onsubmit to<BR>onsubmit="return validate(this);"<BR>or whatever
function name you want.<BR>[color=blue]<BR>> <input
type="submit" value="Submit Application"
onClick="if(this.value<BR>> == 'Continue')
this.form.submit();">[/color]<BR><BR>Give the submit button a
name so we can find and disable it.<BR><BR>Good
luck<BR>/L<BR>--<BR>Lasse Reichstein Nielsen - <A
href="http://bytes.com/forum/register.php" rel=nofollow>Join
Bytes!</A><BR>DHTML Death Colors:
<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html><BR>'Faith
without judgement merely degrades the spirit divine.'<BR></DIV><!-- / message -->
<DIV style="FONT-SIZE: 9pt; MARGIN: 20px 0px 10px"><!-- controls -->
<DIV
style="text-decorations: underline"></DIV><!-- / controls --></DIV><!-- message, attachments, sig --></DIV></DIV><!-- / post #311169 --><!-- post #311169 -->
<DIV
style="BORDER-TOP: #999 1px solid; MARGIN-BOTTOM: 15px; MARGIN-TOP: 5px; PADDING-BOTTOM: 50px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 20px"><!-- user info -->
<DIV><!-- avatar float left --><A
href="http://bytes.com/forum/member.php?u=" rel=nofollow><IMG
alt="Michael Winter's Avatar" border=0 class=avtr
src="password field validation - help - JavaScript_files/noavatar.gif"></A>
<DIV id=postmenu_311169>
<DIV style="FONT-SIZE: 9pt">Michael Winter <SPAN
style="FONT-WEIGHT: normal"><A
href="http://bytes.com/forum/post311169-3.html" rel=nofollow
style="FONT-SIZE: 9pt" target=new>July 20th, 2005 01:26 PM</A>
</SPAN></DIV>
<DIV style="COLOR: #666; FONT-SIZE: 9pt">Guest - n/a Posts </DIV>
<DIV style="COLOR: #666; FONT-SIZE: 9pt">#3: Re: password field
validation - help</DIV></DIV></DIV><!-- / user info --><BR>
<DIV style="FONT-SIZE: 10pt; MARGIN-LEFT: 64px"><!-- message, attachments, sig --><!-- message -->
<DIV style="PADDING-TOP: 10px">Mike wrote on 29 Nov
2003:<BR>[color=blue]<BR>> I've been trying for the past week to
put a simple code<BR>> together. I have done a LOT of searching,
found scripts showing<BR>> the functions I would like to use,
however when I mix them it<BR>> all goes wrong, somehow I always
end up with error messages and<BR>> functions not working right.
Can someone please help me?<BR>><BR>> I have a form, inside is
1 Text Field and 2 Password Fields.<BR>> What I'm looking to do
is:<BR>> - Make sure password fields are equal<BR>> - Set
Minimum/Maximum amount of characters in each field<BR>> - Disable
submit button after clicking submit[/color]<BR><BR>That's a
dangerous thing to do. If a user clicks the 'Back' button,<BR>the
submit button will still be disabled (control states are
usually<BR>saved) and the user has to go back again, then forward to
restore the<BR>initial settings. It might not matter in this case,
though (due to<BR>the nature of the form).<BR>[color=blue]<BR>> I
plan to add more text fields in the future. Will I be able
to?<BR>> I know how to add it in the form, but how do I change
the<BR>> script?[/color]<BR><BR>The text fields have nothing to
do with the validation of the<BR>password fields so you could add
anything you like and it will have<BR>no effect, or does the number
of characters condition you state above<BR>apply to literally
/every/ field? If so, and the limits are the same<BR>for all fields,
or all types of field (60 for password types, 30 for<BR>text, 1000
for textarea, etc), there is no problem. If not (the<BR>limits vary
from field to field) it could become a little more<BR>complicated.
You'll have to explain more about the changes you
might<BR>make.<BR>[color=blue]<BR>> <script
language="JavaScript">[/color]<BR><BR>There is no need to use the
language attribute (it is, in fact,<BR>discouraged), but you /must/
use type (type="text/javascript")<BR>[color=blue]<BR>>
<!--[/color]<BR><BR>You don't need to use SGML comments in SCRIPT
blocks.<BR>[color=blue]<BR>> function checkPw(element) {<BR>>
pw1 = document.FormName.pw1.value;[/color]<BR><BR>You should use the
forms and elements collections, like so:<BR><BR>pw1 =
document.forms['FormName'].elements['pw1'].value;<BR><BR>It aids in
compatibility. You should also use the var keyword to<BR>declare the
variable, otherwise it ends up becoming a
global.<BR>[color=blue]<BR>> pw2 = element.value;<BR>> if (pw1
!= pw2) {<BR>> alert ("Passwords do not match. Please
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -