更新到以前的验证脚本(学校项目)
Update to previous validation script (SCHOOL PROJECT)
好吧,我有一个小问题,因为我读错了部分作业,实际上需要以任何有效的电子邮件地址格式登录,并将其转发到第二个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
处理程序就可以工作了。
-
将你的小提琴改为HEAD,而不是加载
-
您需要删除所有的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
}
}
- 正在将数据主题添加到所有项目
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 如何检查管道中未定义的项目
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 学生搜索项目jquery/javascript
- 如何获取不属于我项目的上一页的URL
- Dojo:访问dijit.form.Select中单击的项目
- 角度的项目列表 ng 重复,ng 单击显示全宽描述
- 我需要为我的朋友在这个项目上提供帮助
- 如何从 HTML 查询中删除项目
- 在Meteor项目中安装并包含npm模块后出错
- 当在Ember中点击一个项目时,我如何将一个活动类添加到项目列表中
- 将单击事件添加到附加的项目中
- 通过另一个php应用程序将我的项目推送到Github存储库中
- 在javascript中搜索项目列表的性能
- 如何将本地依赖项添加到npm项目中
- 在学校项目中使用jquery插件是剽窃吗
- 在HTML5画布中为学校项目构建一个表情符号
- 更新到以前的验证脚本(学校项目)
- 美术学校项目