JavaScript - Password Validation
I am struggling trying to get this bit of code I've written to work. It should validate a password that is 10 characters long, starts with a D and includes 1 *. Even if I blank out two of the while statements and only use the charAt while statement it still fails to loop back. Any ideas?
function validatePassword() { //declare and initialise variables var intCount = 0; var intCharCount = 7; var intAstrixCount =0; var strPassword = parseInt(prompt("Please enter password", "")); while (strPassword.length < intCount) { alert ("Your password must be at least 10 characters long"); strPassword = parseInt(prompt("Please enter password", "")); intCount++; } while (strPassword.charAt(0) != "a") { alert ("Your password does not start with a D"); strPassword = parseInt(prompt("Please enter password", "")); intCount++; } while (intAstrixCount != 1) { alert ("Your password does not contain an *"); strPassword = parseInt(prompt("Please enter password", "")); intCount++; } alert ("Your password is correct"); } Similar Tutorialsfunction StrongPassword(Input) { //var re = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,}/; //1 number,1 Uppercase,1 lowercase,6 characters var re = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/; //1 digit,1 Uppercase,1 lowercase if (re.test(Input)) { alert('Strong Password'); } else { alert('Weak pwd'); } } Help to get correct output.... both the expr will execute the else block only... Working on college assignment, cant figure it out and need help! Basically a form that asks the user to fill in a valid email and password (ill give them the password), doesnt matter if it isnt secure, or if the email accepts dodgy emails (im just verifying the @ and a . and that is ok for the purpose of this. Anyway been trying with this code for ages now and cant see what is wrong but it isnt working, ive swapped it around and at times it does check that it is a valid email, sometimes it allows empty inputs. here is the code <script type="text/javascript"> function validateForm() { var x=document.forms["myForm"]["email"].value var y=document.forms["myForm"]["password"].value var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("Not a valid e-mail address"); return false; } </script> I think i need something like && (y!="123") around the 'if' statement, but not a clue how or where it goes. Basically im going to set the password to 123 so dont have to allow for caps etc. Easiest way to do this, anyone plleeeeeaaasseee?? Kind regards Alan I'm having trouble getting two password boxes to work on the same page, which I created using the JavaScript Kit Encrypted Password Generator (http://www.javascriptkit.com/epassword/index.htm). I've used the code that this generator produces, with some modifications as given by cheesebagpipe (http://www.codingforums.com/showthread.php?t=14646). These changes enable the user to press the keyboard's enter key to submit the password (as an alternative to clicking the submit button), and will also refocus the text box and select the text in it if the user enters the wrong password. The code works fine on pages with just one box (e.g. http://www.mistymusic.co.uk/StLuke's.html), but what changes are needed to make two work on the same page? (I'm new to web design and clueless when it comes to JavaScript, which I know isn't the most secure method of password protection, but will do for now). At the moment, on pages with two boxes (e.g. http://www.mistymusic.co.uk/BEAP.html), neither of the boxes work; this appears in the address bar instead: 'http://www.mistymusic.co.uk/BEAP.html?password2=help!' ('help!'=whatever has been entered in the text box). The full code for both boxes is given below. Code: <div id="passwordBoxes"> <div id="password2"> <form name="password1" onsubmit="submitentry();return false;"> <span class="WhiteLogin">Keyboard classes login</span> <input name="password2" type="password" class="AlignedFormBoxes" size="15" /> <input type="button" class="AlignedFormBoxes" value="Login" /> </form> </div> <div id="password1"> <form name="password1" onsubmit="submitentry();return false;"> <span class="WhiteLogin">Community Youth Choir login</span> <input name="password2" type="password" class="AlignedFormBoxes" size="15" /> <input type="button" class="AlignedFormBoxes" value="Login" /> </form> </div> </div> <script> var pass=new Array() var t3="" var lim=6 pass[0]="gc3Gu5uygvU8VDA" pass[1]="aNL6yjrU5AAKyy3" pass[2]="enSaX8glavp54H" pass[3]="WF3XUKEpbbMEx" pass[4]="8Pv7bsEqaIjNdb6b" pass[5]="35Pv7bsEqaIjNdb6" //configure extension to reflect the extension type of the target web page (ie: .htm or .html) var extension=".html" var enablelocking=0 var numletter="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" var temp3='' var cur=0 function max(which){ return (pass[Math.ceil(which)+(3&15)].substring(0,1)) } function testit(input){ temp=numletter.indexOf(input) var temp2=temp^parseInt(pass[phase1-1+(1|3)].substring(0,2)) temp2=numletter.substring(temp2,temp2+1) return (temp2) } function submitentry(){ t3='' verification=document.password1.password2.value phase1=Math.ceil(Math.random())-6+(2<<2) var indicate=true for (i=(1&2);i<window.max(Math.LOG10E);i++) t3+=testit(verification.charAt(i)) for (i=(1&2);i<lim;i++){ if (t3.charAt(i)!=pass[phase1+Math.round(Math.sin(Math.PI/2)-1)].charAt(i)) indicate=false } if (verification.length!=window.max(Math.LOG10E)) indicate=false if (indicate) window.location=verification+extension; else { alert("That wasn't the correct password. Please check that your CAPS Lock key is turned off and try again."); document.password1.password2.focus(); document.password1.password2.select(); } } </script> <script> var pass=new Array() var t3="" var lim=7 pass[0]="Qmsn8tUQZX4T0PC" pass[1]="DUtntGtFhaTppWO" pass[2]="JahI7L4JqbxZrwS" pass[3]="ong1qqnbr9rvV2s" pass[4]="7BlqC2sc8hQ8bWMK" pass[5]="15BlqC2sc8hQ8bWM" //configure extension to reflect the extension type of the target web page (ie: .htm or .html) var extension=".html" var enablelocking=0 var numletter="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" var temp3='' var cur=0 function max(which){ return (pass[Math.ceil(which)+(3&15)].substring(0,1)) } function testit(input){ temp=numletter.indexOf(input) var temp2=temp^parseInt(pass[phase1-1+(1|3)].substring(0,2)) temp2=numletter.substring(temp2,temp2+1) return (temp2) } function submitentry(){ t3='' verification=document.password1.password2.value phase1=Math.ceil(Math.random())-6+(2<<2) var indicate=true for (i=(1&2);i<window.max(Math.LOG10E);i++) t3+=testit(verification.charAt(i)) for (i=(1&2);i<lim;i++){ if (t3.charAt(i)!=pass[phase1+Math.round(Math.sin(Math.PI/2)-1)].charAt(i)) indicate=false } if (verification.length!=window.max(Math.LOG10E)) indicate=false if (indicate) window.location=verification+extension; else { alert("That wasn't the correct password. Please check that your CAPS Lock key is turned off and try again."); document.password1.password2.focus(); document.password1.password2.select(); } } </script> Any help would be a lifesaver - thanks in advance! How would you make a password feild and a retype password feild and they have to be the same?
Hey all. I have a simple validation I need to do. I need to just make sure that a Checkbox is checked, and that a Text field has content. Sounds simple but I cannot find any thing that has a check and a text field. Here what I have. Can I modify this script to do this? A Checkbox MUST be checked and Text field MUST be filled out. This currently does the text field fine, but no Checkbox obviously. How can I add a checkbox validation to this? Thats it. Any help is appreciated. Code: <script type="text/javascript"> var textFields = ["digsig"]; function validateForm( ) { var oops = ""; // must initialize this! var form = document.sig; for ( var t = 0; t < textFields.length; ++t ) { var field = form[textFields[t]]; var value = field.value.replace(/^\s+/,"").replace(/\s+$/,""); // trim the input if ( value.length < 1 ) { oops += "You MUST enter your Digital Signature"; } } if ( oops != "" ) { alert("ERROR:" + oops); return false; } } } </script> Hello all, new here Seems like a very nice place to be apart of. I have my website www.gebcn.com. If you view source you will see all that I have done, but more importantly my problem. I have the JS code at the top there and I am unable to W3C validate my HTML because of the JS. I am using XHTML strict and would like to stay using it. The JS I have at the top is my form validation code. I am able to do any validating that I need with this "snippet" of code, I have shrank it from my library version just to use for this newsletter. Until now W3C validating was not important now for some reason it is and I am faced with this problem. I am not a Javascript guy more of a HTML/CSS guy and I can manipulate JS to suit my needs. <problem> I have tried to make this "snippet" of JS code an external file but receive multiple errors with the JS calling for the FORM NAME as it is not on the same page. The form NAME=NEWSLETTER is another problem, as W3C says I am unable to use attribute "NAME" in this location. <problem> I would like to keep the JS close to how it is now as I have a library to use this JS over and over again. Any pointers in the right direction or solutions to my problem would be greatly appreciated. Thanks! Hopefully it is not to hard huh If there is anything anyone needs, the code pasted here, or anything else please let me know. Thanks again! Hello! I'm doing an assignment that validates a password field, the password field should only contain numbers, however this code only verifies the first number input in the field, which means i need to check through every singel input in the field, so i got a little problem <script> function checkPassword(strng) { var text = strng.value; var regel = text.replace(/[\(\)\.\-\ ]/g, ''); if(isNaN(parseInt(text))) { window.alert("Fel, password is not a number:" + text); } } else { window.alert("correct e-mail and password!"); } } </script> I've been thinking about a for loop should fix the problem, but im new to javascript, some help would be appreciated! I want to be able to put up files and assign passwords to individuals so that they can access one of those files but not all. In other words person A enters password & is redirected to file A; person B enters password & is redirected to file B, etc. Anyone have an example or know where I can get an example of a script like this? I am not a programmer so the simplier the better. Thanks
I know this is not very secure, but there is a problem with this and I am not sure how to go on. Code: <form> <input type="text" name="password"/> <input type="button" onclick="check()"/> </form> What is wrong with my javascript? Code: function check (){ if(password=="pass1"){ alert('Password is correct!') } } I have a password check on my join form but it looks ugly the idea is good but it is all unaligned and makes the form look bad. I was wondering if you guys can help me format it i tried my self but it just caused it to not work at all lol. Right now it looks like this: ---------------------------- | -------- bar color -------- | ---------------------------- GOOD PASSWORD What i want is it to appear like this: __________________________ | -------- Good Pass -------- | <- Color in the bg,words in the box! Code: (function(A){A.extend(A.fn,{pstrength:function(B){var B=A.extend({verdects:["Very weak","Weak","Medium","Strong","Very strong"],colors:["#f00","#c06","#f60","#3c0","#3f0"],scores:[10,15,30,40],common:["password","sex","god","123456","123","cute","gamer","qwerty","monkey", "apple"],minchar:6},B);return this.each(function(){var C=A(this).attr("id");A(this).after("");A(this).after("<div class=\"pstrength-info\" id=\""+C+"_text\"></div>");A(this).after("<div class=\"pstrength-bar\" id=\""+C+"_bar\" style=\"margin-top: 5px; border: 1px solid gray; font-size: 1px; height: 15px; width: 260px;\"></div>");A(this).keyup(function(){A.fn.runPassword(A(this).val(),C,B)})})},runPassword:function(D,F,C){nPerc=A.fn.checkPassword(D,C);var B="#"+F+"_bar";var E="#"+F+"_text";if(nPerc==-200){strColor="#000";strText="Unsafe password word!";A(B).css({width:"0%"})}else{if(nPerc<0&&nPerc>-199){strColor="#ccc";strText="Too short";A(B).css({width:"5%"})}else{if(nPerc<=C.scores[0]){strColor=C.colors[0];strText=C.verdects[0];A(B).css({width:"10%"})}else{if(nPerc>C.scores[0]&&nPerc<=C.scores[1]){strColor=C.colors[1];strText=C.verdects[1];A(B).css({width:"25%"})}else{if(nPerc>C.scores[1]&&nPerc<=C.scores[2]){strColor=C.colors[2];strText=C.verdects[2];A(B).css({width:"50%"})}else{if(nPerc>C.scores[2]&&nPerc<=C.scores[3]){strColor=C.colors[3];strText=C.verdects[3];A(B).css({width:"75%"})}else{strColor=C.colors[4];strText=C.verdects[4];A(B).css({width:"92%"})}}}}}}A(B).css({backgroundColor:strColor});A(E).html("<span style='color: "+strColor+";'>"+strText+"</span>")},checkPassword:function(C,B){var F=0;var E=B.verdects[0];if(C.length<B.minchar){F=(F-100)}else{if(C.length>=B.minchar&&C.length<=(B.minchar+2)){F=(F+6)}else{if(C.length>=(B.minchar+3)&&C.length<=(B.minchar+4)){F=(F+12)}else{if(C.length>=(B.minchar+5)){F=(F+18)}}}}if(C.match(/[a-z]/)){F=(F+1)}if(C.match(/[A-Z]/)){F=(F+5)}if(C.match(/\d+/)){F=(F+5)}if(C.match(/(.*[0-9].*[0-9].*[0-9])/)){F=(F+7)}if(C.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)){F=(F+5)}if(C.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)){F=(F+7)}if(C.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)){F=(F+2)}if(C.match(/([a-zA-Z])/)&&C.match(/([0-9])/)){F=(F+3)}if(C.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)){F=(F+3)}for(var D=0;D<B.common.length;D++){if(C.toLowerCase()==B.common[D]){F=-200}}return F}})})(jQuery) Sorry about the code i know its cluttered lol. Hey I'm a beginner so bare with me, don't have much knowledge of Javascript so any help would be great Im trying to write coding for entering a password into a prompt box, Ive got the prompt to work however I only want the password to work if 7 characters are entered, here is what I have: var strPromptBox; var strPasswordLength; function validatePassword() { strPromptBox=prompt ("Please enter your password below") } while (strPasswordLength <char(6) || strPasswordLength >char(6) ) { alert ("Please try again"); } I know its wrong and probably not even on the right lines of what I should be doing so any help would be great I am fairly new to Javascript. From what I can compare to sample codes this should work. I have a button with an onclick that opens a prompt. Compares whats written to a php SESSION variable. If true redirects, if false displays an alert. However, all of this means nothing because when I press the button, no prompt comes up! Heres the code Code: <head> <script type="text/javascript"> function passPrompt() { var pass=prompt("Please enter your password:","") if pass = <?php $pwd ?> header('Location: setup.php'); else alert("Password is incorrect"); } </script> </head> ... <input id="settings" type="button" name="settings" value="Settings" onclick="passPrompt()" /> I have used the Java Script from your site: http://www.javascriptkit.com/howto/htaccess3.shtml Evertime I enter the correct UserName and Password I get this Error: User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; Media Center PC 6.0) Timestamp: Sun, 16 Jan 2011 01:42:45 UTC Message: A security problem occurred. Line: 21 Char: 1 Code: 0 URI: file:///C:/test/htmllogin/login.htm OK, so I used this password protection.. http://www.javascriptkit.com/script/cut10.shtml But I need to use it more than once on a page.. and it doesn't work by just changing the password and file it points to... http://unaschade.com/portfolio.html So, is there any other part of the code I can change in order to use this script on the page more than once? Or does anyone have a better suggestion for me? Thanks in advance! On the 'Edit Profile' page of my eCommerce Wordpress Theme (http://www.projectdisobey.com/disobeyclothing), no error message appears when I do any of the following in the ''New Password' and 'Confirm New Password' text fields: 1. 'New Password' and 'Confirm New Password' do not match. 2. 'Confirm New Password' field is left empty. 3. New password is less than 5 characters. 4. New password is exactly the same as the current password. This is the script: Code: <script language="javascript" type="application/javascript"> function chk_form_pw() { if(document.getElementById('new_passwd').value == '') { alert("<?php _e(ENTER_NW_PW_JS_MSG) ?>"); document.getElementById('new_passwd').focus(); return false; } if(document.getElementById('new_passwd').value.length < 4 ) { alert("<?php _e(ENTER_NW_PW_MIN_JS_MSG) ?>"); document.getElementById('new_passwd').focus(); return false; } if(document.getElementById('cnew_passwd').value == '') { alert("<?php _e(ENTER_CONFIRMNW_PW_JS_MSG) ?>"); document.getElementById('cnew_passwd').focus(); return false; } if(document.getElementById('cnew_passwd').value.length < 4 ) { alert("<?php _e(ENTER_CONFIRMNW_PW_MIN_JS_MSG) ?>"); document.getElementById('cnew_passwd').focus(); return false; } if(document.getElementById('new_passwd').value != document.getElementById('cnew_passwd').value) { alert("<?php _e(ENTER_NW_AND_CONFIRM_PW_SAME_JS_MSG) ?>"); document.getElementById('cnew_passwd').focus(); return false; } } </script> I'd be extremely appreciative if anybody could help me fix the script so that it works as it is meant to. Javascript is not something I'm particularly familiar with... Thanks in advance! I have a simple password protection setup on my site but the password is displayed when it is typed in is there a way to change it to a # or * when it is typed in
Hoping someone can help a simple soul!! I am trying to work out if there is a way that I can use a target url as a simple password (or even username) at a log in screen. Essentially I want to be able to send people to a different URL depending on their login details but without having to do anything too complicated or secure. Hoping someone can help Thanks in advance! Hi there... actually i am working on a project "random password generator". What basically is to be done in this project is to generate user password for the username randomly he has provided. Then, this password is to be stored in a text file in the following manner UserName Password ABCD JU12H6 Sdjksj HJudfdsfd Now my problem is that every time I just try to generate a new password, either a new file is made or the heading is rewritten again and again or if I just try to change the variables true and false, then the username and password headings are not printed. Please help. If someone knows how to do this using if-else method then please tell me, how it can be done? The code is attached. Code: <html> <HEAD> <SCRIPT LANGUAGE="JavaScript"> function GetSelectedItem() { chosen = ""; for (i = 0; i <3; i++) { if (document.myform.opt[i].checked) { chosen = document.myform.opt[i].value; } } if (chosen == "") { alert("Choose your password type") exit; } return chosen; } function getPassword(usnm,length,opti) { if(usnm=="") { alert("Enter user name"); exit; } var optsel=GetSelectedItem(); var num = "0123456789"; var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; var alphanum = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var pass = ""; if(length!=5 && length!=6 && length!=7 && length!=8 && length!=9 && length!=10) { alert("Enter password length between 5 and 10"); exit; } else { if (optsel==1) { for(x=0;x<length;x++) { i = Math.floor(Math.random() * 72); pass += alphanum.charAt(i); } } else if (optsel==2) { for(x=0;x<length;x++) { i = Math.floor(Math.random() * 10); pass += num.charAt(i); } } else if (optsel==3) { for(x=0;x<length;x++) { i = Math.floor(Math.random() * 52); pass += alpha.charAt(i); } } WriteToFile(usnm,pass); return pass; } } function WriteToFile(usnm,pass) { try { var fso1=new ActiveXObject("Scripting.FileSystemObject"); var pf1=fso1.OpenTextFile("Password.text",8); //if(pf=='false') //var fso=new ActiveXObject("Scripting.FileSystemObject"); //dspf=fso.OpenTextFile("Password.txt",8,true); //pf.WriteLine("Username\t\tPassword"); pf1.WriteLine(usnm+'\t\t'+pass); pf1.Close(); } catch(err) { var fso=new ActiveXObject("Scripting.FileSystemObject"); pf=fso.CreateTextFile("Password.txt",8,false); pf.WriteLine("Username\t\tPassword") pf.WriteLine(usnm+'\t\t\t'+pass); pf.Close(); } // fso= null; /* else { //var fso = new ActiveXObject("Scripting.FileSystemObject"); var s = fso.CreateTextFile("D:\\Password.txt", true); s.WriteLine('Username\t\tPassword'); //while(s.next()) s.WriteLine(usnm+'\t\t'+pass); s.Close(); }*/ /*var fso = new ActiveXObject("Scripting.FileSystemObject"); var s = fso.OpenTextFile("D:\\Password.txt",8, true); s.WriteLine('Username\t\tPassword'); //while(s.next()) s.WriteLine(usnm+'\t\t'+pass); s.Close();*/ } </script> </HEAD> <BODY> <center> <table width=80% border=0> <tr align=center> <td> <form name="myform"> <table border=0> <tr> <td> Enter Username: </td> <td> <input type=text name=userName value="" size=20> </td> </tr> <tr> <td> Chooose Password type:</td> <td> <input type="radio" name="opt" value="1" >Alphanumeric <input type="radio" name="opt" value="2">Numeric <input type="radio" name="opt" value="3">Alphabetic </td> </tr> <tr> <td> </tr> <tr> <td> Password length: </td> <td> <input type=text name=passwordLength value="" size=2> </td> </tr> </table> </td> </tr> <tr align=center> <td> New password: <input type=text name=password size=20> <br> <input type=button value="Generate password" onClick="document.myform.password.value = getPassword(document.myform.userName.value,document.myform.passwordLength.value, document.myform.opt.checked);"> </form> </td> </tr> </table> </center> </html> I was wondering, could you have more than one username and password for the Username and Password II script? and if so, could you tell me how?
I'm working on a script to check password strength "onkeyup",so when the user enters a password after every character i call a function to check the passwords strength.I'm almost ready,but i stucked with one thing: I want to check the same characters in the password,so i can deduct some point from the score if there are identical characters. Here is my code: PHP Code: var multis = 0; for(var x = 1; x < passLength; x++) { actChar = new RegExp(password.charAt(x),"g"); multis += password.match(actChar).length; } I know it seems an easy task,but i've spent at least 6 hours with it,(the first pitfall was that i didn't know the RegExp function and took my variables into the match() function,i think you can imagine the results)and i just can't figure this out,i always get crazy results,i've tried a few more versions as well without any luck. Thx in advance for anybody who can help me! |