Javascript表单验证警报

时间:2015-07-07 03:24:08

标签: javascript

这是HTML

<input class="btn" onClick="return IsEmpty()" type="submit" name='submit' value="submit"/>

javascript

function IsEmpty(){
  if(document.forms['form'].name.value == "")
  {
    alert("empty");
    return false;
  }
  if(document.forms['form'].name.value != ""){
    alert('Thank You, Your order is being processed');
    return true;
    }
}   

我想要做的是只有填写完所有字段才能显示感谢警报。目前,该表单捕获了缺少的输入,但仍然会发出感谢信息。

2 个答案:

答案 0 :(得分:4)

请尝试:

    function IsEmpty() {
        if (document.forms['form'].name.value == "") {
            alert("empty");
            return false;
        }
        alert('Thank You, Your order is being processed');
        return true;
    }

DEMO

答案 1 :(得分:3)

试试这个

&#13;
&#13;
function IsEmpty(){
  if(document.forms['form'].name.value == "")
  {
    alert("empty");
    return false;
  }
  if(document.forms['form'].name.value != ""){
    alert('Thank You, Your order is being processed');
    return true;
    }
}   
&#13;
<form name="form" action="">
    <input name="name" />
    <input class="btn" onClick="return IsEmpty()" type="submit" name='submit' value="submit" />
</form>
&#13;
&#13;
&#13;