javascript 检查javascript中的文本框是否为空白
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11557577/
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
Check if textbox is blank in javascript
提问by Narendra
I have a email textbox in my web page. When user submits the page I am doing some client side validation using javascript. I can get the value of the textbox and compare it to know if it has any data or not. But in case user enters only blank spaces how will I bypass it.
我的网页中有一个电子邮件文本框。当用户提交页面时,我正在使用 javascript 进行一些客户端验证。我可以获取文本框的值并进行比较以了解它是否有任何数据。但如果用户只输入空格,我将如何绕过它。
I am using below mentioned code.
我正在使用下面提到的代码。
// Check the format of email only if it has some data.
// otherwise no checking is needed.
if ($('#txtEmail').val() != "")
{
// do something
}
Thanks in advance.
提前致谢。
回答by bugwheels94
Use $.trim()
function to remove all white-space from beginning and end of the string
使用$.trim()
函数从字符串的开头和结尾删除所有空格
if ($.trim($('#txtEmail').val()) != "")
{
// do something
}
回答by Pranay Rana
javascript
javascript
if((document.getElementById('bereich').value).length==0)
or jquery
或 jquery
if ($('#txtEmail').val()).length==0)
or
或者
if ($.trim($('#txtEmail').val()).length==0)