gpt4 book ai didi

JavaScript 验证 :

转载 作者:行者123 更新时间:2023-11-28 00:55:59 32 4
gpt4 key购买 nike

我遇到了这个问题,这些验证从电子邮件验证部分开始就停止工作。

我只是不明白为什么即使经过几天的观察,只是想知道是否有人可以在这里指出我的错误?

Javascript部分:

function validateForm()
{
var x=document.forms["myForm"]["firstname"].value;

if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}

var x=document.forms["myForm"]["lastname"].value;

if (x==null || x=="")
{
alert("Last name must be filled out");
return false;
}

var x=document.forms["myForm"]["age"].value;

if (x==null || x=="" || x < 18 || x > 110 || isNaN(x))
{
alert("Age must be 18-110");
return false;
}

var x=document.forms["myForm"]["email"].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;
}

var x=document.forms["myForm"]["phone"].value;
var pattern = /^\(?([0-9]{2})\)?[ ]+([0-9]{4})[ ]+([0-9]{4})$/;

if (x==null || x=="")
{
alert("Not a valid phone number");
return false;
} else if (x.match(pattern)) {
return true;
} else {
alert ("Phone number must be in the following format: xx xxxx xxxx");
return false;
}
}

这是表单部分:

<form id="myForm" action="../includes/create-user.php" onsubmit="return validateForm();" method="post">
<div id = "table">
<table>
<tr>
<td>First Name: </td>
<td><input type="text" name="firstname" id="firstname"/></td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type="text" name="lastname" id="lastname"/></td>
</tr>
<tr>
<td>Age: </td>
<td><input type="text" name="age" id="age"/></td>
</tr>
<tr>
<td>E-mail (username): </td>
<td><input type="text" name="user" id="user1"/></td>
</tr>
<tr>
<td>Password: </td>
<td><input type="password" name="pass" id="pass1"/></td>
</tr>
<tr>
<td>Phone: </td>
<td><input type="text" name="phone" id="phone"/></td>
</tr>
</table>
</div>
<div>
<input type="submit" name="submit" id="submit" value="Register"/>
</div>
</form>

最佳答案

首先值得注意的是您指定您的字段:

<td><input type="text" name="user" id="user1"/></td>

id = 'user1'然后您在脚本中查找电子邮件

var x=document.forms["myForm"]["email"].value;

关于JavaScript 验证 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26216905/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com