简体   繁体   English

如何在HTML文本框中检查null值?

[英]how to check null value in html textbox?

I have a JSP page where i am fetching value from html textbox. 我有一个JSP页面,我在其中从html文本框中获取值。
I want to insert that value into MySQL database. 我想将该值插入MySQL数据库。 I want if textbox in empty,null,zero and undefined then insert 0 in database otherwise actual value insert in database. 我想如果文本框为空,空,零和未定义,则在数据库中插入0,否则在数据库中插入实际值。

Here is my code. 这是我的代码。

Strins s1=request.getparameter("edate");
s1="1990-07-16 09:12:45"
int s4=0

<script>
    var a=<%=s1%>
    if(a==null || a==undefined || a=='' || a==0){                
       <% psmnt.setInt(6,s4); %>
    }
    else{
       <% psmnt.setString(6,s1); %>
    }
</script>

You can use this 你可以用这个

<script>
if(typeof a == 'undefined' || a == 0) {
// Insert db 0
}
</script>

Is your texbox <texarea> or <input type="text"> ? 您的texbox是<texarea>还是<input type="text">

If you are using <input type="text"> , put default value on it: 如果您使用的是<input type="text"> ,请在其上<input type="text">默认值:

 <input type="text" value="">

While for <textarea> , <textarea></textarea> should give your default value "" 对于<textarea><textarea></textarea>应该提供默认值""

So that you only need to check 这样您只需要检查

if(a==''){
    ......
}

Or if you didn't allow white space too, do this: 或者,如果您也不允许使用空格,请执行以下操作:

if(a=='' || a.trim() ==''){
    ......
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM