Javascript 文本区域空检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10849421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:13:05  来源:igfitidea点击:

Textarea Empty check

javascripttextarea

提问by Jhilom

HTML

HTML

<form name="myForm" id="myForm" onsubmit="return validate();" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" name="name" id="name">
<textarea name="details" id="details"></textarea>
<input type="submit">
</form>

Javascript

Javascript

function validate()
{
         if(document.getElementById('details').value == '') 
         {      
        alert("Please Provide Details!");
              document.getElementById('details').focus();
        return false;       
        }
         else if(document.getElementById('name').value == '')
         {      
        alert("Please Provide Name!");
               document.getElementById('name').focus();
        return false;       
        }
         else
           return true;
}

OR

或者

function validate()
    {
             if(document.myForm.details.value == '') 
             {      
            alert("Please Provide Details!");
              document.myForm.details.focus();
            return false;       
            }
             else if(document.myForm.name.value == '')
             {      
            alert("Please Provide Name!");
              document.myForm.name.focus();
            return false;       
            }
             else
               return true;
    }

I have seen the codes from previous Stack Overflow but as I am using these and it is not working. Will anyone help to solve check empty Value on Textarea using Javascript but not jquery.

我已经看过之前 Stack Overflow 的代码,但是当我使用这些代码时,它不起作用。任何人都可以帮助解决使用Javascript而不是jquery在Textarea上检查空值的问题。

The Reference I have used how to check the textarea content is blank using javascript?And How to check if a Textarea is empty in Javascript or Jquery?

我使用的参考资料 如何使用 javascript 检查 textarea 内容是否为空白?以及 如何检查 Javascript 或 Jquery 中的 Textarea 是否为空?

回答by Jhilom

I think you may have space problem on your textarea. Use a trim function to reduce that. Here is the example following. I hope it may solve your problem.

我认为您的 textarea 可能有空间问题。使用修剪功能来减少它。以下是示例。我希望它可以解决您的问题。

JavaScript Add this function

JavaScript 添加此功能

function trimfield(str) 
{ 
    return str.replace(/^\s+|\s+$/g,''); 
}

And your JavaScript function

还有你的 JavaScript 函数

function validate()
{
     var obj1 = document.getElementById('details');
     var obj2 = document.getElementById('name');
         if(trimfield(obj1.value) == '') 
         {      
              alert("Please Provide Details!");
              obj1.focus();
              return false;       
         }
         else if(trimfield(obj2.value) == '')
         {      
               alert("Please Provide Name!");
               obj2.focus();
               return false;       
        }
         else
           return true;
}

OR

或者

function validate()
    {
         var obj1 = document.myForm.details;
         var obj2 = document.myForm.name;
             if(trimfield(obj1.value) == '') 
             {      
               alert("Please Provide Details!");
               obj1.focus();
               return false;       
             }
             else if(trimfield(obj2.value) == '')
             {      
               alert("Please Provide Name!");
               obj2.focus();
               return false;       
             }
             else
               return true;
    }

And HTML with PHP

和 HTML 与 PHP

<form name="myForm" id="myForm" onsubmit="return validate();" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" name="name" id="name">
<textarea name="details" id="details"></textarea>
<input type="submit">
</form>

回答by isuru

You can use following jQuery to escape white spaces.

您可以使用以下 jQuery 来转义空格。

if($("#YourTextAreaID").val().trim().length < 1)
{
    alert("Please Enter Text...");
    return; 
}