javascript 中的验证 - 删除焦点上的错误消息

发布于 2024-08-31 12:35:49 字数 1481 浏览 1 评论 0原文

我不太熟悉 javascript,所以请耐心等待。

我有一个表单,其中我使用 JavaScript 验证控件。当通过 div 字段为空时,会显示错误,但是当我聚焦并在文本框中键入内容时,div 应该消失。但错误 div 没有,即使我输入有效的内容,它仍然显示 div。

我想知道这个脚本哪里出了问题:

<script type="text/javascript">
var err = document.getElementById("errmsg");

function checkInput(inPut) {

    if (inPut.getValue() == "") {
            err.setStyle('display', 'block');
            err.setTextValue("Field cannot be empty!");
            inPut.focus();
            return false;
    }
    else {
            return true;
    }

}


function checkTextBox(textBox)
{

    if (textBox.getValue() == "") {
          err.setStyle('display', 'block');
          err.setTextValue("Field cannot be empty!");
          textBox.focus();
         return false;
    }     
    else if (!checkValidity(textBox.getValue())) {
                 err.setStyle('display', 'block');
         err.setTextValue("Please enter a valid email address!");
         textBox.focus();
         return false;
    }
    else {
         return true;
    }
}

. . . 
<div id="errmsg" class="invalid" style="display:none;"></div> <br />

. . . 
<input type="text" tabindex="1" name="name" id="name" class="input_contact" onblur="checkInput(this);"/>  <br />

. . . 
<input type="text" tabindex="2" name="email" id="email" class="input_contact" onblur="checkTextBox(this);"/>  <br />

它是 facebook 应用程序中的一个表单,但是当 fbjs 工作时,我认为我的基本 javascript 有问题。

i'm not very well-versed with javascript, so please bear with me.

i've a form in which i validate the controls with javascript. the error is displayed when the fields are empty via a div, but when i focus and type something in the textbox, the div should go away. but the error div doesn't and even if i type something valid, it still displays the div.

i'd like to know where am i going wrong with this script:

<script type="text/javascript">
var err = document.getElementById("errmsg");

function checkInput(inPut) {

    if (inPut.getValue() == "") {
            err.setStyle('display', 'block');
            err.setTextValue("Field cannot be empty!");
            inPut.focus();
            return false;
    }
    else {
            return true;
    }

}


function checkTextBox(textBox)
{

    if (textBox.getValue() == "") {
          err.setStyle('display', 'block');
          err.setTextValue("Field cannot be empty!");
          textBox.focus();
         return false;
    }     
    else if (!checkValidity(textBox.getValue())) {
                 err.setStyle('display', 'block');
         err.setTextValue("Please enter a valid email address!");
         textBox.focus();
         return false;
    }
    else {
         return true;
    }
}

. . . 
<div id="errmsg" class="invalid" style="display:none;"></div> <br />

. . . 
<input type="text" tabindex="1" name="name" id="name" class="input_contact" onblur="checkInput(this);"/>  <br />

. . . 
<input type="text" tabindex="2" name="email" id="email" class="input_contact" onblur="checkTextBox(this);"/>  <br />

it's a form in facebook app but while the fbjs works, i assume there's a problem with my basic javascript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只涨不跌 2024-09-07 12:35:49

试试这个

var err = document.getElementById("errmsg");

function checkInput(inPut) {

    if (inPut.getValue() == "") {
            err.setStyle('display', 'block');
            err.setTextValue("Field cannot be empty!");
            inPut.focus();
            return false;
    }
    else {
            err.setStyle('display', 'none');
            err.setTextValue("");
            return true;
    }

}


function checkTextBox(textBox)
{

    if (textBox.getValue() == "") {
          err.setStyle('display', 'block');
          err.setTextValue("Field cannot be empty!");
          textBox.focus();
         return false;
    }     
    else if (!checkValidity(textBox.getValue())) {
         err.setStyle('display', 'block');
         err.setTextValue("Please enter a valid email address!");
         textBox.focus();
         return false;
    }
    else {
         err.setStyle('display', 'none');
         err.setTextValue("");
         return true;
    }
}

try this

var err = document.getElementById("errmsg");

function checkInput(inPut) {

    if (inPut.getValue() == "") {
            err.setStyle('display', 'block');
            err.setTextValue("Field cannot be empty!");
            inPut.focus();
            return false;
    }
    else {
            err.setStyle('display', 'none');
            err.setTextValue("");
            return true;
    }

}


function checkTextBox(textBox)
{

    if (textBox.getValue() == "") {
          err.setStyle('display', 'block');
          err.setTextValue("Field cannot be empty!");
          textBox.focus();
         return false;
    }     
    else if (!checkValidity(textBox.getValue())) {
         err.setStyle('display', 'block');
         err.setTextValue("Please enter a valid email address!");
         textBox.focus();
         return false;
    }
    else {
         err.setStyle('display', 'none');
         err.setTextValue("");
         return true;
    }
}
dawn曙光 2024-09-07 12:35:49

为了让 div 在您第一次输入内容时消失,而不是在检查字段时消失,您还需要字段的 onchange 和/或 onfocus 事件处理程序:

<input type="text" tabindex="1" name="name" id="name" class="input_contact"
    onblur="checkInput(this);"
    onfocus="err.setStyle('display', 'none');"
    onchange="err.setStyle('display', 'none');"
/>

如果您愿意,也可以在 checkInput() 内部设置它们。

To get the div to disappear when you first type something, instead of when the field is checked, you'll also need onchange and/or onfocus event handlers for the fields:

<input type="text" tabindex="1" name="name" id="name" class="input_contact"
    onblur="checkInput(this);"
    onfocus="err.setStyle('display', 'none');"
    onchange="err.setStyle('display', 'none');"
/>

They could also be set inside checkInput(), if you so desire.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文