⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 password field validation - help - javascript.htm

📁 password field validation - help - JavaScript.htm
💻 HTM
📖 第 1 页 / 共 5 页
字号:
            now, the passwords have been validated. The form can be<BR>&gt; // 
            submitted, and the submit button (I've named it 'apply'<BR>&gt; // 
            for this example) can be disabled.<BR>&gt; 
            form.elements['apply'].disabled = true;<BR>&gt; return true;<BR>&gt; 
            }<BR>&gt; &lt;/SCRIPT&gt;<BR>&gt; &lt;/HEAD&gt;<BR>&gt;<BR>&gt; 
            &lt;BODY&gt;<BR>&gt; &lt;FORM name="FormName" method="post"<BR>&gt; 
            action="http://www.photoshelf.net/cgi-bin/mailto"<BR>&gt; 
            onsubmit="return checkPw(this)"&gt;<BR>&gt; &lt;INPUT type="text" 
            name="UserName" size="20" maxlength="60"&gt;<BR>&gt; &lt;INPUT 
            type="password" name="pw1" size="20" maxlength="60"&gt;<BR>&gt; 
            &lt;INPUT type="password" name="pw2" size="20" 
            maxlength="60"&gt;<BR>&gt; &lt;INPUT type="submit" name="apply" 
            value="Submit Application"&gt;<BR>&gt; &lt;/BODY&gt;<BR>&gt; 
            &lt;/HTML&gt;<BR>&gt;<BR>&gt; Hope this helps,<BR>&gt; 
            Mike<BR>&gt;<BR>&gt; Sorry, I didn't mean to write this for you, but 
            there were quite a<BR>&gt; few mistakes creeping in that would take 
            too much explanation to fix.<BR>&gt; I think I was giving you too 
            much new information without explaining<BR>&gt; it properly. My 
            fault.[/color]<BR><BR><BR><BR>Excellent, worked beautiful, I can 
            take it from here! Thanks a lot,<BR>you saved me a lot of time. 
            Thanks.<BR>Mike<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 #311784 --><!-- post #311784 -->
            <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_311784>
            <DIV style="FONT-SIZE: 9pt">Mike <SPAN 
            style="FONT-WEIGHT: normal"><A 
            href="http://bytes.com/forum/post311784-7.html" rel=nofollow 
            style="FONT-SIZE: 9pt" target=new>July 20th, 2005 01:34 PM</A> 
            </SPAN></DIV>
            <DIV style="COLOR: #666; FONT-SIZE: 9pt">Guest - n/a Posts </DIV>
            <DIV style="COLOR: #666; FONT-SIZE: 9pt">#7: 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) wrote in message 
            news:&lt;5eee303f.0312021816.a8fb836@posting.google.com&gt;...[color=blue]<BR>&gt; 
            Michael Winter &lt;M.Winter@blueyonder.co.uk.invalid&gt; wrote in 
            message 
            news:&lt;Xns944496C0A542DMWinterBlueyonder@193.38.113.46&gt;...[color=green]<BR>&gt; 
            &gt; Mike wrote on 30 Nov 2003:<BR>&gt; &gt;[color=darkred]<BR>&gt; 
            &gt; &gt; &lt;html&gt;<BR>&gt; &gt; &gt; &lt;head&gt;<BR>&gt; &gt; 
            &gt; &lt;title&gt;Test Page 01&lt;/title&gt;<BR>&gt; &gt; &gt; 
            &lt;META http-equiv="Content-Script-Type"<BR>&gt; &gt; &gt; 
            content="text/javascript"&gt; &lt;script 
            type="text/javascript"&gt;<BR>&gt; &gt; &gt; function 
            checkPw(element) {<BR>&gt; &gt; &gt; pw1 = 
            document.forms['FormName'].elements['pw1'].value;<BR>&gt; &gt; 
            &gt;<BR>&gt; &gt; &gt; pw2 = element.value;<BR>&gt; &gt; &gt; if 
            (pw1 != pw2) {<BR>&gt; &gt; &gt; window.alert ("Passwords do not 
            match. Please<BR>&gt; &gt; &gt; re-enter."); 
            document.FormName.pw1.value="";<BR>&gt; &gt; &gt; window.alert 
            ("Please enter a password.");<BR>&gt; &gt; &gt; 
            document.FormName.pw2.value="";<BR>&gt; &gt; &gt; window.alert 
            ("Please re-enter your password.");<BR>&gt; &gt; &gt; 
            document.FormName.pw1.focus();<BR>&gt; &gt; &gt; }<BR>&gt; &gt; &gt; 
            // --&gt;<BR>&gt; &gt; &gt; &lt;/script&gt;<BR>&gt; &gt; &gt; 
            &lt;/head&gt;<BR>&gt; &gt; &gt; &lt;body&gt;<BR>&gt; &gt; &gt; 
            &lt;form action="http://www.photoshelf.net/cgi-bin/mailto"<BR>&gt; 
            &gt; &gt; onsubmit="return FrontPage_Form1_Validator(this)" 
            method="post"<BR>&gt; &gt; &gt; name="FormName"&gt;<BR>&gt; &gt; 
            &gt; &lt;input type="text" name="UserName" size="20" 
            maxlength="60"&gt;<BR>&gt; &gt; &gt; &lt;input type="password" 
            name="pw1" size="20" maxlength="60"&gt;<BR>&gt; &gt; &gt; &lt;input 
            type="password" name="pw2" size="20" maxlength="60"<BR>&gt; &gt; 
            &gt; "this.value"&gt;&lt;br&gt;<BR>&gt; &gt; &gt; &lt;input 
            type="submit" value="Submit Application"<BR>&gt; &gt; &gt; 
            onSubmit="if(this.value == 'Continue') 
            this.form.submit();"&gt;<BR>&gt; &gt; &gt; &lt;/body&gt;<BR>&gt; 
            &gt; &gt; &lt;/html&gt;[/color]<BR>&gt; &gt;<BR>&gt; &gt; If this is 
            exactly what you are testing, it's because the function<BR>&gt; &gt; 
            (used in the form onsubmit event) FrontPage_Form1_Validator() 
            doesn't<BR>&gt; &gt; exist.<BR>&gt; &gt;<BR>&gt; &gt; You should 
            remove the closing SGML comment (// --&gt;) at the end of 
            the<BR>&gt; &gt; script block.<BR>&gt; &gt;<BR>&gt; &gt; Finally, 
            you should remove the onsubmit event on the submit button.<BR>&gt; 
            &gt; INPUT elements (of any type) don't have such an event, so it 
            means<BR>&gt; &gt; and does nothing.<BR>&gt; &gt;<BR>&gt; &gt; 
            Here's some advice to help you do the other things you asked about 
            in<BR>&gt; &gt; your original post:<BR>&gt; &gt;<BR>&gt; &gt; You've 
            already got the maximum lengths restricted in your HTML. To<BR>&gt; 
            &gt; set a minimum length, use [control reference].value.length. 
            For<BR>&gt; &gt; example:<BR>&gt; &gt;<BR>&gt; &gt; 
            document.forms['FormName'].elements['pw1'].value.length<BR>&gt; 
            &gt;<BR>&gt; &gt; To disable the submit button, place the following 
            at the end of the<BR>&gt; &gt; validation script so it will be 
            executed when the form validates<BR>&gt; &gt; correctly:<BR>&gt; 
            &gt;<BR>&gt; &gt; 
            document.forms['FormName'].elements['submitButtonName'].disabled 
            =<BR>&gt; &gt; true;<BR>&gt; &gt;<BR>&gt; &gt; If you make all these 
            changes and add a proper onsubmit event handler<BR>&gt; &gt; for the 
            form, your document above would look something like:<BR>&gt; 
            &gt;<BR>&gt; &gt; &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 
            4.01//EN"<BR>&gt; &gt; 
            "http://www.w3.org/TR/html4/strict.dtd"&gt;<BR>&gt; &gt;<BR>&gt; 
            &gt; &lt;HTML&gt;<BR>&gt; &gt; &lt;HEAD&gt;<BR>&gt; &gt; &lt;META 
            http-equiv="Content-Script-Type" 
            content="text/javascript"&gt;<BR>&gt; &gt;<BR>&gt; &gt; 
            &lt;TITLE&gt;Test Page 01&lt;/TITLE&gt;<BR>&gt; &gt;<BR>&gt; &gt; 
            &lt;SCRIPT type="text/javascript"&gt;<BR>&gt; &gt; function 
            checkPw(form) {<BR>&gt; &gt; var pw1 = form.elements['pw1'];<BR>&gt; 
            &gt; var pw2 = form.elements['pw2'];<BR>&gt; &gt; var 
            minPasswordLength = 6;<BR>&gt; &gt;<BR>&gt; &gt; // Check password 
            length<BR>&gt; &gt; if ((minPasswordLength &gt; 
            pw1.value.length)<BR>&gt; &gt; || (minPasswordLength &gt; 
            pw2.value.length)) {<BR>&gt; &gt; window.alert(<BR>&gt; &gt; 
            'Passwords must be at least ' + minPasswordLength<BR>&gt; &gt; + ' 
            characters long.');<BR>&gt; &gt; pw1.focus();<BR>&gt; &gt; return 
            false;<BR>&gt; &gt; }<BR>&gt; &gt;<BR>&gt; &gt; // Check password 
            match<BR>&gt; &gt; if (pw1.value != pw2.value) {<BR>&gt; &gt; 
            window.alert(<BR>&gt; &gt; 'The passwords do not match. Please 
            re-enter.');<BR>&gt; &gt; pw1.value = '';<BR>&gt; &gt; pw2.value = 
            '';<BR>&gt; &gt; pw1.focus();<BR>&gt; &gt; return false;<BR>&gt; 
            &gt; }<BR>&gt; &gt;<BR>&gt; &gt; // By now, the passwords have been 
            validated. The form can be<BR>&gt; &gt; // submitted, and the submit 
            button (I've named it 'apply'<BR>&gt; &gt; // for this example) can 
            be disabled.<BR>&gt; &gt; form.elements['apply'].disabled = 
            true;<BR>&gt; &gt; return true;<BR>&gt; &gt; }<BR>&gt; &gt; 
            &lt;/SCRIPT&gt;<BR>&gt; &gt; &lt;/HEAD&gt;<BR>&gt; &gt;<BR>&gt; &gt; 
            &lt;BODY&gt;<BR>&gt; &gt; &lt;FORM name="FormName" 
            method="post"<BR>&gt; &gt; 
            action="http://www.photoshelf.net/cgi-bin/mailto"<BR>&gt; &gt; 
            onsubmit="return checkPw(this)"&gt;<BR>&gt; &gt; &lt;INPUT 
            type="text" name="UserName" size="20" maxlength="60"&gt;<BR>&gt; 
            &gt; &lt;INPUT type="password" name="pw1" size="20" 
            maxlength="60"&gt;<BR>&gt; &gt; &lt;INPUT type="password" name="pw2" 
            size="20" maxlength="60"&gt;<BR>&gt; &gt; &lt;INPUT type="submit" 
            name="apply" value="Submit Application"&gt;<BR>&gt; &gt; 
            &lt;/BODY&gt;<BR>&gt; &gt; &lt;/HTML&gt;<BR>&gt; &gt;<BR>&gt; &gt; 
            Hope this helps,<BR>&gt; &gt; Mike<BR>&gt; &gt;<BR>&gt; &gt; Sorry, 
            I didn't mean to write this for you, but there were quite a<BR>&gt; 
            &gt; few mistakes creeping in that would take too much explanation 
            to fix.<BR>&gt; &gt; I think I was giving you too much new 
            information without explaining<BR>&gt; &gt; it properly. My 
            fault.[/color]<BR>&gt;<BR>&gt;<BR>&gt;<BR>&gt; Excellent, worked 
            beautiful, I can take it from here! Thanks a lot,<BR>&gt; you saved 
            me a lot of time. Thanks.<BR>&gt; Mike[/color]<BR><BR>One last 
            thing, to control minimum length on text field, I should add<BR>the 
            line below to the script 
            right?<BR><BR>document.forms['FormName'].elements['UserName'].value.length<BR><BR>Where 
            in the script? Does location matter?<BR>Thanks again. Mike<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 #311806 --><!-- post #311806 -->
            <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_311806>
            <DIV style="FONT-SIZE: 9pt">Michael Winter <SPAN 
            style="FONT-WEIGHT: normal"><A 
            href="http://bytes.com/forum/post311806-8.html" rel=nofollow 
            style="FONT-SIZE: 9pt" target=new>July 20th, 2005 01:35 PM</A> 
            </SPAN></DIV>
            <DIV style="COLOR: #666; FONT-SIZE: 9pt">Guest - n/a Posts </DIV>
            <DIV style="COLOR: #666; FONT-SIZE: 9pt">#8: 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 06 Dec 
            2003:<BR><BR>&lt;snipped script&gt;<BR>[color=blue]<BR>&gt; One last 
            thing, to control minimum length on text field, I<BR>&gt; should add 
            the line below to the script right?<BR>&gt;<BR>&gt; 
            document.forms['FormName'].elements['UserName'].value.length<BR>&gt;<BR>&gt; 
            Where in the script? Does location matter?[/color]<BR><BR>The 
            minimum length is already checked by the script I 
            wrote:<BR><BR>function checkPw(form) {<BR>var pw1 = 
            form.elements['pw1'];<BR>var pw2 = form.elements['pw2'];<BR>var 
            minPasswordLength = 6;<BR><BR>// Check password length<BR>if 
            ((minPasswordLength &gt; pw1.value.length)<BR>|| (minPasswordLength 
            &gt; pw2.value.length)) {<BR>window.alert(<BR>'Passwords must be at 
            least ' + minPasswordLength<BR>+ ' characters 
            long.');<BR>pw1.focus();<BR>return false;<BR>}<BR><BR>// Omitted the 
            rest of the script...<BR><BR>If you want to change the minimum 
            length, change the initial value of<BR>the minPasswordLength 
            variable. If you want to impose minimum limits<BR>on other fields, 
            copy that if statement block and alter it as needed.<BR>For 
            example:<BR><BR>// To check username length<BR>var username = 
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -