javascript错误消息未显示

时间:2012-07-26 01:01:48

标签: javascript

如果文本框(First_Name)保持为空,我试图发出错误。例如,如果firstname字段留空,则用户将收到以下错误:“请输入您的名字”在我将字段留空后单击提交按钮时没有任何反应...

<HTML>
<HEAD>
<TITLE>this is a test page</TITLE>
<script language="javascript" type="text/javascript">
<!--
function CheckForm()
{
  var formObj = document.getElementById("Data");
  var firstname = formObj.FIRST_Name.value;

    if(notEmpty(firstname, "Please enter your first name")){                        

    return true;}

    return false;
    }

function notEmpty(elem, helperMsg){
    if(elem.value.length == 0){
        alert(helperMsg);
        elem.focus(); // set the focus to this input
        return false;
    }
    return true;
}

</script>
</HEAD>
<BODY>
<div style="background: #CCCC99">
<HR><FORM id="Data" onsubmit="return CheckForm()" >
<P>First Name: <input type=text name=FIRST_Name maxlength=15 size=15>
</P>
<input type=submit value="Submit Products Registration Form" style="width: 220px"><input type=reset value="Reset">
</form>
</div>
</BODY>
</HTML>

1 个答案:

答案 0 :(得分:2)

firstname是元素值,但您将其作为元素传递给notEmpty,而是使用

if(notEmpty(formObj.FIRST_Name, "Please enter your first name")){