gpt4 book ai didi

JavaScript 验证不起作用?

转载 作者:行者123 更新时间:2023-11-28 13:51:26 25 4
gpt4 key购买 nike

它出了什么问题,为什么它不起作用......

 <script language="JavaScript" type="text/javascript">


//function to check empty fields

function isEmpty(strfield1, strfield2) {
//change "field1, field2 and field3" to your field names

strfield1 = document.forms[0].name.value
strfield2 = document.forms[0].email.value

//name field

if (strfield1 == "" || strfield1 == null || !isNaN(strfield1) || strfield1.charAt(0) == ' ') {
alert( "Name is a mandatory field.\nPlease amend and retry.")
return false;
}

//EMAIL field
if (strfield2 == "" || strfield2 == null || !isNaN(strfield2) || strfield2.charAt(0) == ' ') {
alert(" Email is a mandatory field.\nPlease amend and retry.")
return false;
}
return true;
}


//function to check valid email address

function isValidEmail(strEmail){
validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
strEmail = document.forms[0].email.value;

// search email text for regular exp matches

if (strEmail.search(validRegExp) == -1) {
alert('A valid e-mail address is required.\nPlease amend and retry');
return false;
}
return true;
}


//function that performs all functions, defined in the onsubmit event handler

function check(form)){
if (isEmpty(form.field1)){
if (isEmpty(form.field2)){
if (isValidEmail(form.email)){
return true;
}
}
}
}
return false;
}

</script>

它没有做任何事情,我不明白那里发生了什么,我也把它放在形式中

<form onsubmit="return check(this);" action="sendquery.php" name="contquery">

最佳答案

乍一看:@FishBasketGordo 所示的括号太多,所以我不会重复

第二眼 - 您传递了该字段,但没有测试该字段值

第三眼:您没有将正确的名称传递给函数

第四眼 - isEmpty 在为空时返回 false。它应该返回 true

我修复了所有这些

DEMO HERE

完整页面显示内容的去向。已更新以在表单上进行不显眼的事件处理

<html>
<head>
<title>Validation</title>
<script type="text/javascript">

// trim for IE
if(typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}

//function to check empty fields

function isEmpty(objfld) {
var val = objfld.value;
if (val.trim() == "" || val == null) {
alert(objfld.name+" is a mandatory field.\nPlease amend and retry.");
objfld.focus();
return true;
}
return false;
}

//function to check valid email address

function isValidEmail(objEmail){
var validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
var strEmail = objEmail.value;
if (strEmail.match(validRegExp)) return true;
alert('A valid e-mail address is required.\nPlease amend and retry');
objEmail.focus();
return false;
}

//function that performs all functions, defined in the onsubmit event handler

function validate(form) {
if (isEmpty(form.name)) return false;
if (isEmpty(form.email)) return false;
return isValidEmail(form.email);
}


window.onload=function() {
document.getElementById("form1").onsubmit=function() {
return validate(this);
}
}

</head>
<body>
<form id="form1">
Name:<input type="text" name="name" /><br/>
Email:<input type="text" name="email" /><br/>
<input type="submit" />
</form>
</body>
</html>

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

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