このフォームバリデーションを作成して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>