确定用户输入字段是否为空

时间:2017-06-08 11:17:11

标签: javascript

我已经在这里查看了一些答案,但由于某些原因,我无法让这个工作。我希望用户对某些内容进行评分,如果他们在必填字段中没有输入任何内容,请按"评分",我希望它能提醒某些内容。



function Script() {
  var G = document.getElementById("gradevalue").value
  if (G == null || G == undefined) {
    window.alert("Please input a grade");
  }
}

<label id="grade">|Please grade on a scale from 1 to 10 (10 is the best!)         
    <input id="gradevalue" type="text"; size="2"; maxlength="2">
    </label>
<input type="button" value="GRADE!" id="nota" onclick="Script()">
&#13;
&#13;
&#13;

我对此非常陌生,所以请不要太苛刻。

3 个答案:

答案 0 :(得分:2)

我想,你可以简单地检查一下:

if (!Number(G)) {
   window.alert("Please input a grade");
}
如果trueGnullundefined''0,则{p}将为false NaN以及未转换为数字的任何字符串。

function CheckGrade () {
  var G = document.getElementById("gradevalue").value;
  if(!Number(G)) {
    window.alert("Please input a grade");
  } else {
    //your code here, maybe some othe validation logic
  }
}
<label id="grade">|Please grade on a scale from 1 to 10 (10 is the best!)         
<input id="gradevalue" type="number" min="1" max="10"/>
</label>
<input type="button" value="GRADE!" id="nota" onclick="CheckGrade()">

更新添加了代码示例和小错误。

答案 1 :(得分:1)

检查function Script() { var G = document.getElementById("gradevalue").value if(!G || G == 0) { window.alert("Please input a grade"); } }似乎可以完成这项工作:

&#13;
&#13;
<label id="grade">|Please grade on a scale from 1 to 10 (10 is the best!)         
    <input id="gradevalue" type="text"; size="2"; maxlength="2">
</label>
<input type="button" value="GRADE!" id="nota" onclick="Script()">
&#13;
if (!G) {
    window.alert("Please input a grade");
} else if(G == 0 || G > 10) {
    window.alert("Please input a grade between 1 and 10");    
}
&#13;
&#13;
&#13;

请注意,您可能还想检查&gt; 11个值:

Sheets("TimeSeries").Select
Range("TimeSeries[#Headers]").Select
Selection.AutoFilter
ActiveSheet.ListObjects("TimeSeries").Range.AutoFilter Field:=4, Operator:= _
    xlFilterValues, Criteria2:=Array(1, Format(Sheets("Start").Range("C2").Value, "mm/dd/yyyy"))
Range("A1").Select
Selection.End(xlDown).Select
Range(Selection, Selection.End(xlUp)).Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.SpecialCells(xlCellTypeVisible).Offset(0, 0).Select
Selection.EntireRow.Delete

答案 2 :(得分:0)

你也必须在JavaScript中,你可以检查变量是否有赋值给它,如果它没有,那么bool表达式将为false。我使用工作代码https://codepen.io/robdavis/pen/bRVxdm

快速完成了CodePen
function Script() {
  var g = document.getElementById("gradevalue").value

  if (!g) {
    window.alert("Please enter a Grade");
  }
}