-1

我的代码如下:。

<!doctype html>
<html lang="en">
<head>
<title>Testing the textarea</title>
<style type="text/css"></style>
<script type="text/javascript" src="validation.js"></script>
</head>
<body>
<div id="wrapper">
<span id="error_box" style="display:none;"></span>
<form name="storyTeller" method="get" action="#" onSubmit="return validateForm()">
<p class="title">
<label for="title">TITLE:</label>
<input type="text" id="title" name="title" required onBlur="validateTitle(title)"/>
</p>
<textarea name="entry" id="entry" rows="10" cols="45" onBlur="validateEntry(entry)">
</textarea>
<input type="submit" value="submit" />
</form>
</div>
</body>
</html>

validation.js 的内容:

function validateTitle(title){/*validating the title*/
if (isNaN(document.getElementById('title').value)){
document.getElementById('title').style.background="#ccffcc";
document.getElementById('error_box').style.display="none";
return true;
}
else{
document.getElementById('error_box').innerHTML='Please enter a valid title';
document.getElementById('error_box').style.display="block";
document.getElementById('title').style.background="red";
return false;
}
}
function validateEntry(entry){/*validating the entry*/
var x=document.getElementById('entry').value;
x = x.trim();
if((x=="")||(x==null)){
document.getElementById('entry').style.background="red";
document.getElementById('error_box').innerHTML = 'Where is your story';
document.getElementById('error_box').style.display="block";
return false;
}
else{
document.getElementById('entry').style.background="#ccffcc";
document.getElementById('error_box').innerHTML='';
document.getELementById('error_box').style.display="none";
return true;
}
}
function validateForm(){/*validating the form*/
var error = 0;
if(!validateTitle('title')){
document.getElementById('error_box').style.display="block";
error++;
}
if(!validateEntry('entry')){
document.getElementById('error_box').style.display="block";
error++;
}
if(error > 0){
return false;
}
}
4

3 回答 3

0

修改了您的代码,现在它对提交和模糊事件都更有效。
在您的 HTML 源代码中,将这两个onblur属性更改为onblur="validateEntry(this)",然后您可以使用此 javascript:

function validateEntry(entry) { /*validating*/
    var error_box = document.getElementById('error_box');
    error_box.style.display = "none";
    if (entry.name == "title") { // INPUT type="text"
        if (!isNaN(entry.value)) {
            error_box.innerHTML = 'Please enter a valid title';
            error_box.style.display = "block";
            entry.style.background = "#fee";
            return false;
        } else {
            entry.style.background = "#ccffcc";
            return true;
        }
    } else if (entry.name == "entry") { // TEXTAREA
        var x = entry.value.trim();
        if ((x == "") || (x == null)) {
            error_box.innerHTML = 'Where is your story?';
            error_box.style.display = "block";
            entry.style.background = "#fee";
            return false;
        } else {
            entry.style.background = "#ccffcc";
            return true;
        }
    }
}

function validateForm() { /*validating the form*/
    var form = document.forms['storyTeller'];
    var error_box = document.getElementById('error_box');
    var error = 0;
    var error_msg = '';
    if (!validateEntry(form['title'])) {
        error++;
        error_msg += error_box.innerHTML + '<br/>';
    }
    if (!validateEntry(form['entry'])) {
        error++;
        error_msg += error_box.innerHTML;
    }
    if (error > 0) {
        error_box.innerHTML = error_msg;
        error_box.style.display = "block";
        return false;
    } else {
        error_box.style.display = "none";
        return true;
    }
}

jsfiddle

于 2013-06-16T10:12:46.647 回答
0

函数 validateEntry() 中的脚本有错误

换行:

document.getELementById('error_box').style.display="none";

到线:

document.getElementById('error_box').style.display="none";

于 2013-06-16T07:22:05.117 回答
0

将提交按钮设为普通按钮;当点击调用最终验证函数时,在函数结束时,如果一切正常,您必须手动提交表单

例子 :

<script>
function val(){
    if(document.getElementById('tb').value != "")
    document.getElementById('frm').submit();
    else alert('fill the text field');
}
</script>
<form id="frm" action="#">
    <input type="text" id="tb"/>
    <input type="button" id="btn" value="submit" onclick="val()"/>
</form>
于 2013-06-16T06:57:40.967 回答