javascript之input字符串不为空

时间:2021-07-20 16:35:46

今天我们来讲如何判断这个java中字符串输入是否为空

------------------------当只有一个input的时候,我们来进行个判断这个值是否为空------------------------

<div class="form-group">
<label class="col-sm-2 control-label">标题:</label>
<div class="col-sm-4">
<input type="text" id = "title" class="form-control" placeholder="请输入广告标题(必填,10个字以内)" maxlength="10" name="title" value="${adMaterial.text1}"></div>
<div class="col-sm-6 form-control-static"></div>
</div>

<input type="submit" value="提交" class="btn btn-orange" onclick="return checkStr()"></div>

<script>
function checkStr() {
var title = document.getElementById("title").value.trim();
var desc = document.getElementById("desc").value.trim();
if(title == null || title == ""){
alert("标题不能为空");
return false;
}
</script>

  

 

------------------------当有多个input的时候,我们来判断这个一系列的值是否为空-----------------------

<div class="form-group">
<label class="col-sm-2 control-label">标题:</label>
<div class="col-sm-9">
<input type="text" class="form-control" placeholder="10字以内,必填" maxlength="10" name="title" value="${one.title}">
</div>
</div>

<input type="submit" value="提交" class="btn btn-orange" onclick="return checkStr()"></div>

<script>
function checkStr(){
var message = "";
var titles = document.getElementsByName("title");
for(var i = 2 ; i < descs.length ;i++){
var desc = descs[i].value.trim();
if(desc == null || desc ==""){
message += "第"+(i - 2)+"个描述不能为空\r\n";
}
}
if(message != ""){
alert(message);
return false;
}
}
</script>