Javascript在提交到php文件之前没有运行
Javascript not running before submitting to php file
我正在学习更多关于PHP/Javascript,试图建立一个基本的网站。我已经把所有的东西都启动并运行了,但是没有注册验证之类的东西。我试图使用Javascript在提交之前验证表单,但它不适合我。这是我的页面:
<script language="JavaScript" type="text/javascript">
OK = false;
function validateEmail() {
var x = document.forms["form1"]["myemail"].value;
var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if(atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
alert("Not a valid e-mail address");
return false;
}
}
function notShortUsername() {
var x = document.forms["form1"]["myusername"].value;
if(x.value.length < 6) {
// set the focus to this input
OK = false;
}
OK = true;
}
function notShortPassword() {
var x = document.forms["form1"]["mypassword"].value;
if(x.value.length < 6) {
alert(helperMsg);
elem.focus();
// set the focus to this input
OK = false;
}
OK = true;
}
function validate () {
notShortUsername();
notShortPassword();
validateEmail();
if(OK) {
form.submit();
return true;
} else {
//do something else
alert("You did it wrong!");
return false;
}
}
</script>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="registervalidation.php" onsubmit="return validate">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294">
<input name="myusername" type="text" id="myusername" />
</td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td>
<input name="mypassword" type="password" id="mypassword" />
</td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td>
<input name="myemail" type="text" id="myemail">
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td>
<input type="submit" name="Submit" value="Complete Registration">
<input type="submit" name="Submit" value="Cancel">
</td>
</tr>
</table>
</td>
</form>
</tr>
</table>
我也试过使用这个:http://docs.jquery.com/Plugins/Validation
它的问题是当条目失去焦点时它不会验证,并且仍然提交php。
onsubmit="return validate();"
不做忘记();
改变这个:
var x = document.forms["form1"]["myusername"].value;
if(x.value.length < 6) {
:
var x = document.forms["form1"]["myusername"].value;
if(x.length < 6) {
:
var x = document.forms["form1"]["mypassword"].value;
if(x.value.length < 6) {
:
var x = document.forms["form1"]["mypassword"].value;
if(x.length < 6) {
在每种情况下,var x
已经包含该值,因此您可以直接通过x.length
在表单外测试验证函数submit:
<a href="#" onclick="validate();">test</a>
您将在控制台中看到一个错误,告诉您没有定义x。
Uncaught TypeError: Cannot read property 'length' of undefined
因此脚本停止执行并且永远不会返回false,这允许表单发布
相关文章:
- 使用压缩的JavaScript文件(不是运行时压缩)
- Javascript运行php文件,然后下载文件
- 从Chrome扩展名中的popup.html文件在background.js文件中运行一个函数
- 如何从“”运行url;.js”;文件
- 如何使用Gmail运行.js文件
- 如何运行从TFS获取项目的windows脚本文件
- 在Node Webkit应用程序中从DOM单击按钮时运行批处理文件
- 是否可以从javascript文件中将量角器作为模块运行
- Meteor发布/订阅无法从服务器文件夹中运行
- 如何显示由ajax调用运行的PHP文件的echo
- 通过Wordpress中的jquery调用运行php文件
- 从JavaScript文件运行Firebug Lite
- 如何在Grunt.js中观察多个文件,但只在更改的文件上运行任务
- 如何在 node.js 中创建和运行文件
- 如何在使用 nsIProcess 运行文件时隐藏命令提示符窗口.bat
- 文件下载完成后运行文件
- 打开程序但不运行文件的HTA页面
- React-只运行文件一旦外部js文件加载(gapi未定义)
- 从终端运行文件代码(启动本地网站)
- 运行文件.未附加的HTML元素的getElementById