www.webdeveloper.com
Results 1 to 2 of 2

Thread: Need help with Javascript function validation

  1. #1
    Join Date
    May 2013
    Posts
    2

    Need help with Javascript function validation

    I'm a novice in javascript and I'm having trouble with basic stuff. I'm trying to link a javascript function to a html file to validate a login. I want it to only validate 2 email and password combos and direct to another page (index.html) or pop-up an alert if anything else is entered. I think I'm headed in the right direction with my codes but i dont think I'm calling the function properly because it's not doing anything when i enter an email and password. The code I have is below, if anyone can help i would greatly appreciate it. Thanks for your help.

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Home</title>
    <script type="text/javascript" src="login.js"></script>
    </head>
    <body>
    <form method="post" name="login" >
    Email Address: <input type="email" id="email" name="email" required autofocus ><br><br>
    Password: <input type="password" id="password" name="password" required><br><br>
    <input type="submit" value="Submit" onsubmit="validate()">
    </form>
    </body>
    </html>

    -------------------------------------------------------------------------------------------

    function validate(email1, password1, email2, password2)
    {


    var email1=document.login.email.value;
    var email2=document.login.email.value;
    var password1=document.login.password.value;
    var password2=document.login.password.value;
    }
    }
    if ((email1==username@yahoo.com) && (password1==apple1) || (email2==user2@yahoo.com) && (password2==apple2)){

    return 'index.html'
    }

    else alert("Email or password is incorrect, please try again");

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,636
    if you want to make real validation, the way you are going is not the right direction. imagine that the user just type 'http://your.domain.com/index.html' in the address bar and press Enter.

    anyway home.htm
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Home</title>
    <script type="text/javascript" src="login.js"></script>
    </head>
    <body>
    <form method="post" name="login" action="index.htm" onsubmit="return validate()"> 
    Email Address: <input type="email" id="email" name="email" required autofocus /><br><br>
    Password: <input type="password" id="password" name="password" required /><br><br>
    <input type="submit" value="Submit" />
    </form> 
    </body>
    </html>
    login.js

    Code:
    var userdata=[
    ['apple1','username@yahoo.com'],
    ['apple2','user2@yahoo.com']
    ];
    
    function validate(){
    var valid=false,email=document.getElementById('email').value,pass=document.getElementById('password').value;
    for(var i in userdata){if(pass===userdata[i][0] && email===userdata[i][1]){valid=true;break;}else{continue;}}
    return valid;
    }
    Last edited by Padonak; 06-01-2013 at 05:55 AM.
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles