更新到以前的验证脚本(学校项目)

Update to previous validation script (SCHOOL PROJECT)

本文关键字:学校 项目 脚本 验证 更新      更新时间:2023-09-26

好吧,我有一个小问题,因为我读错了部分作业,实际上需要以任何有效的电子邮件地址格式登录,并将其转发到第二个url(myaccount.html(;在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-我们有几个页面,即索引、登录、管理和myaccount(。

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; 

新代码

// 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;

您需要每次从validateForm函数返回false false,以阻止表单自行提交。由于在所有条件下它都是false,所以我将该语句移到了函数的末尾。

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

我还把你的小提琴从onLoad改成了No wrap,这样你自己的onload处理程序就可以工作了。

  1. 将你的小提琴改为HEAD,而不是加载

  2. 您需要删除所有的return语句,并在validate中设置一个return 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 
    }
  }