📄 password field validation - help - javascript.htm
字号:
re-enter.");[/color]<BR><BR>You should qualify that using window.
That is, window.alert(...). The<BR>collections comment above applies
to these, too:<BR>[color=blue]<BR>>
document.FormName.pw1.value="";<BR>>
document.FormName.pw2.value="";<BR>>
document.FormName.pw1.focus();<BR>> return
false;[/color]<BR><BR>Returning false should not be necessary here
(and may not do<BR>anything, anyway). The focus() call should keep
the box in focus.<BR>[color=blue]<BR>> }[/color]<BR><BR>Not that
it matters here, due to what I just wrote, but in future, I<BR>would
suggest that if you return a value for one code path, every<BR>code
path should return a value.<BR>[color=blue]<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">[/color]<BR><BR>You're
using intrinsic events, but you didn't (in this sample) set<BR>the
default scripting language. This is invalid HTML. Place
the<BR>following META element in the document HEAD:<BR><BR><META
http-equiv="Content-Script-Type"
content="text/javascript"><BR>[color=blue]<BR>> <input
type="hidden" name="THANKURL"<BR>>
value="http://www.somepage.org/ok.htm"><BR>> <input
type="hidden" name="RECIPIENT"<BR>>
value="someone@somepage.com">[/color]<BR><BR>This is a /huge/
security risk. Never use a mail form that requires a<BR>full "To:"
address - it's an easy target for spam (both for receiving<BR>it,
and sending it).<BR>[color=blue]<BR>> <input type="text"
name="UserName"<BR>> size="20" maxlength="60"> <input
type="password" name="pw1"<BR>> size="20" maxlength="60">
<input type="password" name="pw2"<BR>> size="20"
maxlength="60"
onBlur="checkPw(this)"><br>[/color]<BR><BR>The check should
really be done upon submission. If I choose to do<BR>something else
before completing a matching password (including<BR>changing my
original password), I'll be trapped in this input field.<BR>There's
also no need to pass the entire object: just pass the value<BR>using
this.value.<BR>[color=blue]<BR>> <input type="submit"
value="Submit Application"<BR>> onClick="if(this.value ==
'Continue') this.form.submit();">[/color]<BR><BR>From what you've
shown, that onclick event doesn't seem to do<BR>anything. It would
have been helpful if you had shown the function<BR>called by the
form's onsubmit event.<BR><BR>Apply the suggestions I've made so
far. If it's not enough, say so,<BR>and include the function called
by the onsubmit event and explain<BR>your restrictions in more
detail, and I'll try again.<BR><BR>Good
luck,<BR>Mike<BR><BR>--<BR>Michael Winter<BR><A
href="http://bytes.com/forum/register.php" rel=nofollow>Join
Bytes!</A>lid (remove ".invalid" to reply)<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 #311243 --><!-- post #311243 -->
<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="Mike's Avatar" border=0 class=avtr
src="password field validation - help - JavaScript_files/noavatar.gif"></A>
<DIV id=postmenu_311243>
<DIV style="FONT-SIZE: 9pt">Mike <SPAN
style="FONT-WEIGHT: normal"><A
href="http://bytes.com/forum/post311243-4.html" rel=nofollow
style="FONT-SIZE: 9pt" target=new>July 20th, 2005 01:27 PM</A>
</SPAN></DIV>
<DIV style="COLOR: #666; FONT-SIZE: 9pt">Guest - n/a Posts </DIV>
<DIV style="COLOR: #666; FONT-SIZE: 9pt">#4: 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">Michael Winter
<M.Winter@blueyonder.co.uk.invalid> wrote in message
news:<Xns9442AC31A84B5MWinterBlueyonder@193.38.113.46>...[color=blue]<BR>>
Mike wrote on 29 Nov 2003:<BR>>[color=green]<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=green]<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=green]<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=green]<BR>> >
<!--[/color]<BR>><BR>> You don't need to use SGML comments
in SCRIPT blocks.<BR>>[color=green]<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=green]<BR>> > pw2 =
element.value;<BR>> > if (pw1 != pw2) {<BR>> > alert
("Passwords do not match. Please
re-enter.");[/color]<BR>><BR>> You should qualify that using
window. That is, window.alert(...). The<BR>> collections comment
above applies to these, too:<BR>>[color=green]<BR>> >
document.FormName.pw1.value="";<BR>> >
document.FormName.pw2.value="";<BR>> >
document.FormName.pw1.focus();<BR>> > return
false;[/color]<BR>><BR>> Returning false should not be
necessary here (and may not do<BR>> anything, anyway). The
focus() call should keep the box in
focus.<BR>>[color=green]<BR>> > }[/color]<BR>><BR>>
Not that it matters here, due to what I just wrote, but in future,
I<BR>> would suggest that if you return a value for one code
path, every<BR>> code path should return a
value.<BR>>[color=green]<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">[/color]<BR>><BR>> You're using intrinsic
events, but you didn't (in this sample) set<BR>> the default
scripting language. This is invalid HTML. Place the<BR>>
following META element in the document HEAD:<BR>><BR>>
<META http-equiv="Content-Script-Type"
content="text/javascript"><BR>>[color=green]<BR>> >
<input type="hidden" name="THANKURL"<BR>> >
value="http://www.somepage.org/ok.htm"><BR>> > <input
type="hidden" name="RECIPIENT"<BR>> >
value="someone@somepage.com">[/color]<BR>><BR>> This is a
/huge/ security risk. Never use a mail form that requires a<BR>>
full "To:" address - it's an easy target for spam (both for
receiving<BR>> it, and sending it).<BR>>[color=green]<BR>>
> <input type="text" name="UserName"<BR>> > size="20"
maxlength="60"> <input type="password" name="pw1"<BR>> >
size="20" maxlength="60"> <input type="password"
name="pw2"<BR>> > size="20" maxlength="60"
onBlur="checkPw(this)"><br>[/color]<BR>><BR>> The
check should really be done upon submission. If I choose to
do<BR>> something else before completing a matching password
(including<BR>> changing my original password), I'll be trapped
in this input field.<BR>> There's also no need to pass the entire
object: just pass the value<BR>> using
this.value.<BR>>[color=green]<BR>> > <input
type="submit" value="Submit Application"<BR>> >
onClick="if(this.value == 'Continue')
this.form.submit();">[/color]<BR>><BR>> From what you've
shown, that onclick event doesn't seem to do<BR>> anything. It
would have been helpful if you had shown the function<BR>> called
by the form's onsubmit event.<BR>><BR>> Apply the suggestions
I've made so far. If it's not enough, say so,<BR>> and include
the function called by the onsubmit event and explain<BR>> your
restrictions in more detail, and I'll try again.<BR>><BR>>
Good luck,<BR>> Mike[/color]<BR><BR>Hi Mike: Without typing
anything on the fields and simply clicking on<BR>submit, IE6
errors:<BR>"Line 22, Char 1, Object Expected"<BR><BR>I'm sure I
screwed
up!:<BR><BR><html><BR><head><BR><title>Test Page
01</title><BR><META http-equiv="Content-Script-Type"
content="text/javascript"><BR><script
type="text/javascript"><BR>function checkPw(element) {<BR>pw1 =
document.forms['FormName'].elements['pw1'].value;<BR><BR>pw2 =
element.value;<BR>if (pw1 != pw2) {<BR>window.alert ("Passwords do
not match. Please
re-enter.");<BR>document.FormName.pw1.value="";<BR>window.alert
("Please enter a
password.");<BR>document.FormName.pw2.value="";<BR>window.alert
("Please re-enter your
password.");<BR>document.FormName.pw1.focus();<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="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>"this.value"><br><BR><input
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -