gpt4 book ai didi

Javascript 表单验证 : the same message is called for every field, 也许是一些愚蠢的打字错误?

转载 作者:行者123 更新时间:2023-11-28 02:34:19 25 4
gpt4 key购买 nike

我正在通过创建此表单验证来练习 Javascript。我只想让每个文本字段都有不同的错误消息。有人能解释一下我做错了什么吗?我没有收到任何错误这是我的代码:

 <html>
<head>
<script language="javascript">
function ReloadTextDiv() {
var NewText = document.getElementById("naam").value;
NewText = NewText.replace(/\n/g, '<br />');
var DivElement = document.getElementById("textDisplay");
DivElement.innerHTML = NewText;
}
function ReloadTextDiv2() {
var NewText = document.getElementById("email").value;
NewText = NewText.replace(/\n/g, '<br />');
var DivElement = document.getElementById("textDisplay2");
DivElement.innerHTML = NewText;
}
function ReloadTextDiv3() {
var NewText = document.getElementById("address").value;
NewText = NewText.replace(/\n/g, '<br />');
var DivElement = document.getElementById("textDisplay3");
DivElement.innerHTML = NewText;
}
</script>
<script type='text/javascript'>
function formValidator(){

var naam = document.getElementById('naam');
var address = document.getElementById('address');
var email = document.getElementById('email');



if(isname(naam, "Please enter only letters for your name")){
if(isAlphanumeric(address, "Numbers and Letters Only for Address")){
if(emailValidator(email, "Please enter a valid email address")){
return true;
}
}
}



return false;

}

function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus();
return false;
}
return true;
}

function isname(elem, helperMsg){
var numericExpression = /^[0-9]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isaddress(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}



function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

</script>
</head>
<body>
<div id="contact_form">
<form onsubmit='return formValidator()' >
<fieldset>
<legend>Subscription info</legend>
<label for="name">Username:</label>
<input type="text" name="naam" id="naam" class="text" onKeyUp="ReloadTextDiv();" />
<br />
<label for="mail">E-mail:</label>
<input type="text" name="email" id="email" class="text" onKeyUp="ReloadTextDiv2();" />
<br />
<label for="address">Address:</label>
<input type="text" name="address" id="address" size="40" class="text" onKeyUp="ReloadTextDiv3();" />
<legend><input type="submit" value="submit"></legend>
</fieldset>
</form>
</div>


<p><span id="textDisplay"></span></p>
<p><span id="textDisplay2"></span></p>
<p><span id="textDisplay3"></span></p>
</body>
<html>

最佳答案

嗯,我认为尝试将名称验证为数字并不是最合理的做法;)。

function isname(elem, helperMsg){
var numericExpression = /^[0-9]+$/; // -> /^[a-zA-Z]+$/
// ...
}

关于Javascript 表单验证 : the same message is called for every field, 也许是一些愚蠢的打字错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13674928/

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