0

好吧,我有一个小问题,我读错了部分作业,实际上需要让这个登录任何有效的电子邮件地址格式并将其转发到第二个 url (myaccount.html)。我已经尝试了几件事,虽然我可以让它登录到 ADMIN 页面和 MYACCOUNT 页面,但如果我在其中输入了无效的电子邮件,它仍然会登录(即 jdelor1965@yahoo.m)。有任何想法吗??谢谢...

// Chapters 3 & 4 - login.js (updated during week 3)

// Function called when the form is submitted.
// Function validates the form data and returns a pop-up if conditions are not met.
function validateForm() {
'use strict';

// Get references to the form elements:
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
var pattern = '/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/';

// Validate!
if (email == 'admin@titanmusicstore.com' && password == 'LogMeIn') 
{
    window.location = "admin.html";
} 

else if (pattern == '/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/' && pattern == '/^\w+@[a-              zA-Z_]+?\.[a-zA-Z]{2,3}$/') 
{
    window.location = "myaccount.html";
} 

else 
{
    alert('Invalid or incorrect Email or Password!');
}
return false;

}
// End of validateForm() function.

JSFiddle:http: //jsfiddle.net/FL2c4/

** 我想知道问题是否是正在使用的 JavaScript 和 HTML 中的“表单操作”之间的冲突 - 电子邮件/密码组合会将我带到“表单操作”字段中列出的页面,但是当我删除该登录信息无处可去?

澄清一下,这是一个学校项目,不能在“现实世界”中使用!已经在这里收到了一些非常好的帮助,所以再次感谢那些提供帮助的人。本周,我们的部分任务是更改我们的登录验证脚本以将两 (2) 个 UID 指向不同的位置。我已经阅读了所有章节,观看了视频,并在网上进行了无休止的研究,但无法弄清楚如何让它发挥作用 - 这就是我所拥有的,任何建议或想法将不胜感激(我也可以提供 HTML 为好吧 - 我们有几个页面,即索引、登录、管理和我的帐户)。

JavaScript:

// Script Week 2 - login.js

// Function called when the form is submitted.
// Function validates the form data and returns a pop-up if conditions are not met.
function validateForm() {
'use strict';

// Get references to the form elements:
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
var url = window.location.toLowerCase('login.html');

// Validate!
if (email == 'admin@titanmusicstore.com' && password == 'LogMeIn')
{
window.location = "admin.html";
}

else if (email == 'jdelor1965@yahoo.com' && password == 'LogMeIn') 
{
window.location = "myaccount.html";
return true;
}

else 
{
    alert('Please fill out form accurately - Incorrect UID or Password!');
    return false;
}

} 
// End of validateForm() function.


// Function called when the window has been loaded.
// Function needs to add an event listener to the form.
function init() {
'use strict';

// Confirm that document.getElementById() can be used:
if (document && document.getElementById) {
    var loginForm = document.getElementById('loginForm');
    loginForm.onsubmit = validateForm;
}

} 
// End of init() function.

// Assign an event listener to the window's load event:
window.onload = init; 
4

2 回答 2

1

新代码

// Script Week 2 - login.js

// Function called when the form is submitted.
// Function validates the form data and returns a pop-up if conditions are not met.
function validateForm() {
    'use strict';

    // Get references to the form elements:
    var email = document.getElementById('email').value;
    var password = document.getElementById('password').value;
    //    var url = window.location.toLowerCase('login.html'); DELETE -- does nothing

    // Validate!
    if (email == 'admin@titanmusicstore.com' && password == 'LogMeIn') {
        window.location = "http://talk.collegeconfidential.com/";
    } else if (email == 'jdelor1965@yahoo.com' && password == 'LogMeIn') {
        window.location = "http://disney.com";
    } else {
        alert('Please fill out form accurately - Incorrect UID or Password!');
    }
    return false;

}
// End of validateForm() function.


// Function called when the window has been loaded.
// Function needs to add an event listener to the form.
function init() {
    'use strict';

    // Confirm that document.getElementById() can be used:
    if (document && document.getElementById) {
        var loginForm = document.getElementById('loginForm');
        loginForm.onsubmit = validateForm;
    }

}
// End of init() function.

// Assign an event listener to the window's load event:
window.onload = init;

您需要false每次从 validateForm 函数返回 false 以阻止表单自行提交。由于它在每种情况下都是错误的,因此我将该语句移到了函数的末尾。

我更改了 URL,以便它们可以在FIDDLE中工作。

我还将您的小提琴从更改为onLoadNo wrap以便您自己的onload处理程序可以工作。

于 2013-11-03T21:27:59.973 回答
0
  1. 将您的小提琴更改为 HEAD 而不是 onload

  2. 您需要删除所有返回语句并在验证中让一个返回 false 像这样http://jsfiddle.net/mplungjan/mt8Vb/

像这样

 window.onload = function () { // when the page has loaded
    // find the form and attach an event handler to the submit
    document.getElementById('loginForm').onsubmit = function () {
      // Get references to the form elements:
      var email = document.getElementById('email').value;
      var password = document.getElementById('password').value;
      // Validate!
      if (email == 'xxx...musicstore.com' && password == 'LogMeIn') {
        window.location = "admin.html";
      } else if (email == 'yyy...yahoo.com' && password == 'LogMeIn') {
        window.location = "myaccount.html";
      } else {
        alert('Please fill out form accurately - Incorrect UID or Password!');
      }
      return false; // you never want to actually submit the form 
    }
  }
于 2013-11-03T21:56:58.687 回答