Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更新以前的验证脚本(学校项目)_Javascript - Fatal编程技术网

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

Javascript 更新以前的验证脚本(学校项目),javascript,Javascript,嗯,我有一个小问题,我读错了作业的一部分,实际上需要让这个登录名使用任何有效的电子邮件地址格式,并将其转发到第二个url(myaccount.html)。我尝试过几种方法,虽然我可以让它同时登录到ADMIN页面和MYACCOUNT页面,但如果我在其中放置了无效的电子邮件,它仍然会登录(即。jdelor1965@yahoo.m). 有什么想法吗??谢谢 // Chapters 3 & 4 - login.js (updated during week 3) // Function cal

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

**我想知道问题是否在于所使用的JavaScript和HTML中的“表单操作”之间存在冲突-电子邮件/passowrd组合会将我带到“表单操作”字段中列出的页面,但当我删除该信息时,登录将不起作用

澄清一下,这是一个学校项目,不用于“现实世界”!我已经收到了一些非常好的帮助,所以再次感谢那些帮助过我的人。本周,我们的部分任务是更改登录验证脚本,将两(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; 
新代码

// 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
,以阻止表单自行提交。因为它在任何情况下都是假的,所以我将该语句移到函数的末尾

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

我还将您的小提琴从
onLoad
改为
No wrap
,这样您自己的
onLoad
处理程序就可以工作了

  • 将你的小提琴改为“头”,而不是“上弦”


  • 您需要删除所有return语句,并在validate中设置一个return false,如下所示

  • 像这样

     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 
        }
      }
    

    谢谢@Jeffman。如前所述进行了更新,但仍然不起作用,尽管我理解您试图传达的关于语法被破坏的信息……您需要发布一个提琴之类的东西。这可能并没有解决你的整个问题,但它必须做到。所以现在你的代码不同了。你可以一直在这里发布更新,但这有点乏味。我最初是这样做的,但在网上搜索后,由于其他地方的一个例子而改变了它。再次感谢,现在尝试一下……好的,在最初的问题中添加了一些额外的背景:我们在主页(index.html)登陆,单击登录链接(login.html),然后根据输入的电子邮件/密码转到两个页面之一(admin.html或myaccount.html)。希望对您有所帮助,并感谢您提供的任何其他帮助…@johnyd65新代码和fiddlevar url=window.location.toLowerCase('login.html');不是有效的语法,它背后的想法是什么?你是说window.location.toLowerCase()+'login.html';您需要删除所有return语句,并在validate中设置一个return false,如下所示-还需要将fiddle改为HEAD,而不是onload@mpligjan就这样。我所做的是:通过阅读和视频教程,遵循我们所能获得的内容,然后在我从各种互联网来源获得的信息中添加一些内容,如果我最初所做的工作不起作用的话。一开始我所拥有的与你提供的更接近,但是你从JS文件的底部删除了所有的信息(那是上周的作业,我不认为我应该删除它)。谢谢你的帮助;我将研究代码和语法,看看哪里出了问题,但它现在确实起作用了!谢谢…似乎应该在控制台中抛出错误…如果他们不在学校教你使用浏览器控制台和/或其他调试工具…深入浏览器控制台…将节省你很多时间和精力grief@charliefl当前位置很少有学校会让学生上一堂深入的课,真正地调试他们的电脑代码。@Jeffman-非常抱歉,我无法投票支持你的任何一个答案,因为我除了是一个新手白痴之外,似乎无法在这里建立任何类型的声誉!请原谅我的无知,我非常努力地去理解这一点,并且真的希望能够。。。