textarea 数据输入检查,如果为空则发出警报

textarea data entry check with alert if empty

我的表单有一个 textarea 字段用于输入。如果提交时该字段为空,我希望弹出警报。

我复制了一些在 input 字段上运行良好的代码;但是,它不适用于 textarea(无论是否为空,它都不会提交任何警报)。
我阅读了此处发布的其他一些问题并进行了一些修改。
现在,它会在空时发出警报,但它也会在有文本且不提交时发出警报。
我是新来的。我正在使用 asp 经典。

代码:

<form method="post" action="reasonProcess.asp" name="formName"  onSubmit="return Validate()">
<table >
<tr>
<td>Please enter a reason for the change:<br>
<textarea style="width:675px;height:75px" rows="12" cols="10" name="changereason" > <%=dbchangereason%> </textarea></td>
</tr>
</table><br>    
<input type=button value="Approve" onClick="javascript:saveAndSubmit()" class="btn" style="float:none;font-size:.78em;">
</form>

<script>
function saveAndSubmit()
{
     // Check for reason entered.
  if (!document.formName.changereason.value == '')
  {
    alert("Enter a reason.");
    return false;
  }
    var queryString = "reasonProcess.asp?Approve=Yes";
    document.formName.action=queryString;
    document.formName.submit();
    // window.close();

 }
</script>

这是与 input 文本字段一起正常工作的代码行:

  if (!document.formName.changereason.value)

我也试过:

 if (!document.formName.changereason.value.length == 0)

并获得不带文本和带文本的警报。

感谢您的帮助。

已更新

'!' is the logical not operator in JavaScript.

代码中的条件是说如果 changereason textarea 的值 不为空 显示 alert() 因为 ! 标志意思是 不是 ,但你想要的是相反的(如果字段为空则显示警报),所以尝试只删除标志并且它会起作用,进行以下更改:

替换:

if (!document.formName.changereason.value == '')
{
     alert("Enter a reason.");
     return false;
}

作者:

if (document.formName.changereason.value == '') //removing the ! sign in this line
{
     alert("Enter a reason.");
     return false;
}

参见Working fiddle

如果这不起作用,请查看 External simple page with the same code,它不是 fiddle,它应该也可以在您的机器上运行,外部示例代码:

<!DOCTYPE html>
<html>
    <head><title></title>head>
    <body>
        <form method="post" action="reasonProcess.asp" name="formName"  onSubmit="return Validate()">
            <table >
                <tr>
                    <td>Please enter a reason for the change:<br>
                        <textarea style="width:675px;height:75px" rows="12" cols="10" name="changereason" > <%=dbchangereason%> </textarea>
                    </td>
                </tr>
            </table>
            <br>    
            <input type=button value="Approve" onClick="javascript:saveAndSubmit()" class="btn" style="float:none;font-size:.78em;">
        </form>

        <script>
            function saveAndSubmit()
            {
                 // Check for reason entered.
                if (document.formName.changereason.value == '')
                {
                     alert("Enter a reason.");
                     return false;
                }

                var queryString = "reasonProcess.asp?Approve=Yes";
                document.formName.action=queryString;
                document.formName.submit();
                // window.close();

             }
        </script>
    </body>
</html>

如果那个工作正常,而不是第一个示例,那么您可能有另一部分代码导致了问题,并且问题中没有引用它。

祝你好运。

改变

 if (!document.formName.changereason.value == '')
 {
alert("Enter a reason.");
return false;
 }

 if (document.formName.changereason.value == '')
 {
alert("Enter a reason.");
return false;
  }

! 表示 "not" - 所以你是说如果值 不是 空然后警报。

在表单标签中写入

<form action=""  method="post" enctype="multipart/form-data" onsubmit="return checkform(this);">

这是您的输入字段

<textarea name="reviewValue"></textarea>

Javascript代码:

<script language="JavaScript" type="text/javascript">
function checkform ( form ){
  if (form.reviewValue.value == "") {
    alert( "Please choice your Rating." );
    form.reviewValue.focus();
    return false ;
  }
  return true ;
}
</script>