好吧,我有一个小问题,我读错了部分作业,实际上需要让这个登录任何有效的电子邮件地址格式并将其转发到第二个 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;