JavaScript - Basic Login Validation
Hi. Firstly I must point out, although I know this probably isnt the best way to create a login system. Its just an example for some of my school work.
Basically, I have a very basic "bare bones" login form. What I want to be able to do, is to create an IF statement that will allow only 1 pair of values for the username and password. eg. username = john password = smith now, I want the if statement to run, and look for the values entered in the username and password fields. if the value for username is correct, and the password is correct, i want another page to open, however if any other values are entered, i want an alert message to be thrown. "please enter correct login details". My problem with this, Is i do not know how to write an if statement that will except one value, and reject all others. Iv included a printscreen of my coding and I warn you, it may be very wrong, but Im confused as to how to make it work. Heres the image of my code: http://i39.tinypic.com/e8sb9j.png Thanks for your time. Similar TutorialsHello, I am a computing pupil currently taking part in a computer science project at GCSE level. Our project is a research task regarding web form validation, JavaScript and embedding JavaScript into HTML code. I have no prior knowledge of any of these topics, so would be very interested in hearing your opinions on the aboce. I would also like to know your opinion on how effective JavaScript validation is, possibly in your field of work, or just your experience of using it? Thanks in advance, Ellen 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 Hello there guys! Thanks for taking time to view this thread. It's appriciated! I found a really nice menu (Javascript, JQuery) im not so high tech on this myself, and not the best coder either but whenever I use this menu it gives the login box on the website an error, it doesn't let people login.. I've tried to localize the error myself, but I just dont know how to fix it, I think the issue is that the site system i use doesn't support these type of scripts which gives the login errors! I'm currently using a free website system, which allows you to customize anything you want called "uCoz" The menu i used was: http://www.dynamicdrive.com/dynamici...enu-glossy.htm And I think it's the jquery that gives the login box errors.. I'm thankfull for answers if anyone knows Hello everyone. I have a a html page that is used as a login page. I know it is not secure but this is just for learning purposes, I'm not using it for anything important. My problem is that when I put in the correct login all that happens is the page just clears the text boxes as if you were starting over. I am trying to get it to go to register.html which is located in the same directory as the login page. Help with what I'm doing wrong would be greatly appreciated. Here is the code: Code: <html> <head> <title>Login Page</title> </head> <body> <script type = "text/javascript"> var count = 2; function validate() { var un = document.myform.username.value; var pw = document.myform.pword.value; var valid = false; var unArray = ["Philip", "George", "Sarah", "Michael"]; // as many as you like - no comma after final entry var pwArray = ["Password1", "Password2", "Password3", "Password4"]; // the corresponding passwords; for (var i=0; i <unArray.length; i++) { if ((un == unArray[i]) && (pw == pwArray[i])) { valid = true; break; } } if (valid) { alert ("Login was successful"); window.location = "register.html"; return false; } var t = " tries"; if (count == 1) {t = " try"} if (count >= 1) { alert ("Invalid username and/or password. You have " + count + t + " left."); document.myform.username.value = ""; document.myform.pword.value = ""; setTimeout("document.myform.username.focus()", 25); setTimeout("document.myform.username.select()", 25); count --; } else { alert ("Still incorrect! You have no more tries left!"); document.myform.username.value = "No more tries allowed!"; document.myform.pword.value = ""; document.myform.username.disabled = true; document.myform.pword.disabled = true; return false; } } </script> <form> <table border ="5"> <tr> <th colspan="2">Login</th> </tr> <tr> <td>Username:</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="pword" /></td> </tr> <tr> <th colspan="2"><input type="Submit" value="Login" name="Submit" onClick="validate()" /><input type="Reset" value="Reset" /></th> </tr> </table> <p> <a href="register.html">New users click here to register</a> </p> </form></body></html> 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! Can't seem to delete.
First post on the forum... I'm brand new to Javascript and I'm teaching myself using the Headfirst HTML5 book. One of the exercises early in the book is asking me to create a musical playlist where I can add songs that will show up on the screen and also be added to the DOM. However, I'm not able to get it to work or see where my error lies. Any feed back would be great. Here's my code: Code: <!doctype html> <html lang="en"> <head> <title>Webville Tunes</title> <meta charset = "utf-8"> <script> function init() { var button = document.getElementById("addButton"); button.onclick = handleButtonClick; window.onload = init; } function handleButtonClick() { var textInput = document.getElementById("songTextInput"); var songName = textInput.value; var li = document.createElement("li"); li.innerHTML= songName; var ul = document.getElementById("playlist"); ul.appendChild(li); alert ("Button was clicked!"); } </script> <link rel = "stylesheet" href="playlist.css"> </head> <body> <form> <input type="text" id="songTextInput" size="40" placeholder="Song name"> <input type="button" id="addButton" value="Add Song"> </form> <ul id="playlist"> </ul> </body> </html> How could a make a basic javascript encoder that can encode and decode text? (i just would want some tips on how to start) Hi all, this is my first attempt at JavaScript code. I'm trying to write code for an Adobe Acrobat form. Here is my situation: I have several boxes, all containing numbers such as 2014, 08, and 000000000. I want to concatenate them all into one long number like 080000000002014. I've looked at a ton of sample code, but nothing I try seems to work. Any help is greatly appreciated.
Hi I'm new to javascript and have a simple question I'm sure somebody can help me with. I have a contact page on my website and trying to validate an email address, but can't get it to work. I want to validate that my fields have been field out and as well, want to validate the email address. Validation of the forms works, however I can't get the email validation working.. What am I doing wrong? <div style="text-align:right;"> <form id="contact" action="contact.php" method="post" autocomplete="off"> <label><em>Name: </em><input type="text" id="name" name="name" class="input"/></label> <label><em>Email : </em><input type="text" id="email" name="email" class="input"/></label> <label><em>Message : </em><textarea name="message" id="message" cols="35" rows="5"></textarea></label> <label>CC Me: <input type="checkbox" name="cc" value="cc"></label> <input type="submit" class="button alignright" name="submit" id="submit" value="Send" style="margin:0"/> </form> </div> <script type="text/javascript"> var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ; $("#contact").submit(function(ev) { if (!(filter.text($email) | ($("#name").val() == "" | $("#email").val() == "" | $("#message").val() == "") )) { $.lightbox().shake(); ev.preventDefault(); } }); </script> Hey guys! New user here and I just need a little help with some basic Javascript I must write a function that 'shows' the corresponding part of the form depending on what the user has selected. So... for example, if I select 'Pizza', the div #section2 with the question about Pizza type should be shown. All the sections that are not selected should be hidden. For example, if 'Mexican' is selected, the sections for 'Pizza' and 'Thai' should be hidden. I want this function to also display a message in the JavaScript console indicating which food type is selected. Apparently I'm supposed to use 'console.log("")'??? I'm using an external javascript file called prac8.js If anyone has any idea how to do this I'd be very greatful Code: <!DOCTYPE html> <html> <head> <title>Prac 8 example</title> <script type="text/Javascript" src="prac8.js"></script> <style> #section2, #section3, #section4 { display:none; border:1px solid gray; padding:8px; margin-top:12px; width:400px; } #dessertmsg { color:green; } </style> </head> <body> <h1>Prac 8 example</h1> <h2>A Food Survey</h2> <form id="survey" action="#" method="post"> <div id="section1"> <label for="food">What is your favourite type of food?</label> <select id="food" onchange="selection()"> <option value="pizza">Pizza</option> <option value="mex">Mexican</option> <option value="thai">Thai</option> </select> </div> <div id="section2"> What is your favourite type of pizza?<br> <label for="hawaiian">Hawaiian</label><input type="radio" id="hawaiian"> <label for="supreme">Supreme</label><input type="radio" id="supreme"> <label for="vegetarian">Vegetarian</label><input type="radio" id="vegetarian"> </div> <div id="section3"> What is your favourite Mexican food?<br> <label for="burrito">Burritos</label><input type="radio" id="burrito"> <label for="chilli">Chilli con carne</label><input type="radio" id="chilli"> <label for="taco">Tacos</label><input type="radio" id="taco"> </div> <div id="section4"> What is your favourite Thai food?<br> <label for="stir">Stir-fry</label><input type="radio" id="stir"> <label for="noodle">Noodles</label><input type="radio" id="noodle"> <label for="curry">Curry</label><input type="radio" id="curry"> </div> <label for="dessert">What is your favourite dessert?*</label> <input type="text" id="dessert" onchange="checkForm()"><br> <div id="dessertmsg"> </div> <input type="submit" value="Submit" id="subbutton" disabled> </form> </body> </html> hey guys, i know this really hot girl and need to make her think we are right for eachother, to do this i need to 'tweak' a lovetest. i know the love test's answer depends on the number of the letters L,O,V,E,Y,O,U but i need an if function that adds up both name boxes and adds to 100%. here is code: // Made by ADAM - roflmao456 var test = ''; function checkLove(){ var l1 = test.GetControlText('love1'); var l2 = test.GetControlText('love2'); first = l1.toUpperCase(); firstlength = l1.length; second = l2.toUpperCase(); secondlength = l2.length; var LoveCount=0; for (Count=0; Count < firstlength; Count++) { letter1=first.substring(Count,Count+1); if (letter1=='L') LoveCount+=2; if (letter1=='O') LoveCount+=2; if (letter1=='V') LoveCount+=2; if (letter1=='E') LoveCount+=2; if (letter1=='Y') LoveCount+=3; if (letter1=='O') LoveCount+=1; if (letter1=='U') LoveCount+=3; } for (Count=0; Count < secondlength; Count++) { letter2=second.substring(Count,Count+1); if (letter2=='L') LoveCount+=2; if (letter2=='O') LoveCount+=2; if (letter2=='V') LoveCount+=2; if (letter2=='E') LoveCount+=2; if (letter2=='Y') LoveCount+=3; if (letter2=='O') LoveCount+=1; if (letter2=='U') LoveCount+=3; } amount=0; if (LoveCount> 0) amount= 5-((firstlength+secondlength)/2) if (LoveCount> 2) amount= 10-((firstlength+secondlength)/2) if (LoveCount> 4) amount= 20-((firstlength+secondlength)/2) if (LoveCount> 6) amount= 30-((firstlength+secondlength)/2) if (LoveCount> 8) amount= 40-((firstlength+secondlength)/2) if (LoveCount>10) amount= 50-((firstlength+secondlength)/2) if (LoveCount>12) amount= 60-((firstlength+secondlength)/2) if (LoveCount>14) amount= 70-((firstlength+secondlength)/2) if (LoveCount>16) amount= 80-((firstlength+secondlength)/2) if (LoveCount>18) amount= 90-((firstlength+secondlength)/2) if (LoveCount>20) amount=100-((firstlength+secondlength)/2) if (LoveCount>22) amount=110-((firstlength+secondlength)/2) if (firstlength==0 || secondlength==0) amount= "Err"; if (amount < 0) amount= 0; if (amount >99) amount=99; test.SetControlText('loveOutput',amount+"%"); return "[c=12]"+test.GetControlText('love1')+"[/c] + [c=29]"+test.GetControlText('love2')+"[/c] = [c=3]"+amount+"[/c]%"; } function OnGetScriptMenu(){ var ScriptMenu = "<ScriptMenu>"; ScriptMenu += "<MenuEntry Id='lovetest'>"; ScriptMenu += "Love Test"; ScriptMenu += "</MenuEntry>"; ScriptMenu += "</ScriptMenu>"; return ScriptMenu; } function OnEvent_MenuClicked(sMenuId){ if(sMenuId == "lovetest") {test = MsgPlus.CreateWnd("windows.xml","tester");} } function sendResult(){ for (var e = new Enumerator(Messenger.CurrentChats); !e.atEnd(); e.moveNext()){ if(test.GetControlText('love1')){ if(test.GetControlText('love2')){ e.item().SendMessage(checkLove()); } else { MsgPlus.DisplayToast("Love Test","Please enter in something in box 2."); } } else { MsgPlus.DisplayToast("Love Test","Please enter in something in box 1.") }}} function OntesterEvent_CtrlClicked(PlusWnd, ControlId){ if(ControlId == "startTest") {checkLove();} else if(ControlId == "sendResults") {sendResult();} else if(ControlId == "BtnClose") {PlusWnd.Close(1);} } END OF CODE please reply with a fixed script with a line like %=50+50 or something so the output is 100%. thanks http://i50.tinypic.com/2njcxn5.png basically.. a button that changes the color of that box.. the font to bold.. and changes a frame from two search engine websites. Hi, I'm trying to create a video box on a website that plays a youtube video and then automatically plays random recommended videos with no break in playback. An endless playlist created by youtube based on the initial video. Youtube uses Javascript API. I have never used Javascript before but i'm just trying to get the video to play as above. There are few tutorials online and I couldn't find anyone trying to do a similar thing. If you have any idea how i'd go about it or if not where i could find some information to help. Thanks. I have this code, (a snippet of it below) which works, but how can I simplify it instead of repeating all the same code for the 12 different checks I have to run. If whatColor == A1 I need the search variable to equal fashion If whatColor == A2 I need the search variable to equal photography etc. Thanks Code: if (whatColor == 'A1') { /* search: 'fashion', */ new TWTR.Widget({ version: 2, type: 'search', search: 'fashion', interval: 2000, title: '', subject: '', width: 310, height: 240, theme: { shell: { background: '#edecee', color: '#444444' }, tweets: { background: '#edecee', color: '#444444', links: '#e80f45' } }, features: { scrollbar: true, loop: false, live: true, hashtags: true, timestamp: true, avatars: true, behavior: 'all' } }).render().start(); } else if (whatColor == 'A2') { /* search: 'photography', */ new TWTR.Widget({ version: 2, type: 'search', search: 'photography', interval: 2000, title: '', subject: '', width: 310, height: 240, theme: { shell: { background: '#edecee', color: '#444444' }, tweets: { background: '#edecee', color: '#444444', links: '#e80f45' } }, features: { scrollbar: true, loop: false, live: true, hashtags: true, timestamp: true, avatars: true, behavior: 'all' } }).render().start(); The Code, is from a twitter widget that pulls a twitter feed based on the search variable. This is the code straight from twitter. Code: <script src="http://widgets.twimg.com/j/2/widget.js"></script> <script> new TWTR.Widget({ version: 2, type: 'search', search: 'photography', interval: 2000, title: '', subject: '', width: 310, height: 240, theme: { shell: { background: '#edecee', color: '#444444' }, tweets: { background: '#edecee', color: '#444444', links: '#e80f45' } }, features: { scrollbar: false, loop: false, live: true, hashtags: true, timestamp: true, avatars: true, behavior: 'all' } }).render().start(); </script> Hi, I've just started learning JavaScript - actually started about half an hour ago. I have an extensive knowledge of Lua (if you've heard of it), so the concepts of programming aren't new to me. This code isn't doing anything. The HTML code makes the hyper-linked pictured show, but it doesn't do anything when the mouse rolls over it. The JavaScript code is saved in scripts/titleImage_rollover.js, and I'll show the HTML code as well. JavaScript: Code: <!-- // image rollover for header function image_rollover(){ //show a set of random images switch(math.floor(math.random()*5+1)) { case 1: document.getElementById("logo").src = "images/t2m_logo.png"; break; case 2: document.getElementById("logo").src = "images/pighead10_halloween.png"; break; case 3: document.getElementById("logo").src = "images/pighead10_easter.png"; break; case 4: document.getElementById("logo").src = "images/pighead10_classic.png"; break; case 5: document.getElementById("logo").src = "images/pighead10_combat.png"; break; } } //reset image to normal function image_reset(){ document.getElementById("logo").src = "images/pighead10.png"; } //--> HTML: < Code: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>pighead10.com | Home</title> <style type="text/css"> .link1 A:link{text-decoration:none;} .link1 A:visited{text-decoration:none;} .link1 A:active{text-decoration:none;} .link1 A:hover{text-decoration:underline;color:blue;} </style> <script type="text/javascript" src="scripts/titleImage_rollover.js"></script> </head> <body style="font-family:arial;color:red"> <a href="http://www.roblox.com/User.aspx?ID=2969907"> <img id="logo" src="images/pighead10.png" width="150" height="200" onmouseover="image_rollover()" onmouseleave="image_reset()"/> </a> </body> </html> Also, is there a way to view some kind of output in JavaScript? Hi, can anyone help me with a simple bit of javascript? I need to modify the following code for a date of birth selector to return false if they date of birth is less than 18 years old. jQuery.validator.addMethod("validDOB", function (str1, element) { var mon1 = parseInt(str1.substring(0,2),10); var dt1 = parseInt(str1.substring(3,5),10); var yr1 = parseInt(str1.substring(6,10),10); var date1 = new Date(parseInt(yr1), parseInt(mon1)-1, parseInt(dt1)); var date2 = new Date(); if(date2 < date1) { return false; } else { return true; } }, "Please enter valid date of birth"); thanks baronne Hi guys, I am new to learning javascript and have a test tomorrow on for loops. I know the answers to the three following loops but i wondered if anyone could take the time to explain to me how to find the answer from the code as I have no idea on how to find the answers (the answer is what is displayed in the alert). Many thanks, Mike The loops are these three: 1. var a=4;b=6; for(var c=0;c<=a;c++){b=b+c;} alert(b); (answer is 16) 2. var v=1; for(i=1;i<5;i++) for(j=2;j<5;j++) v+=1; alert(v); (answer is 13) 3. var s=0; for(i=0;i<=8;i++) if((i%2)==0)s+=1; alert(s); (answer is 5) |