1

如果文本框(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>
4

1 回答 1

2

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

if(notEmpty(formObj.FIRST_Name, "Please enter your first name")){ 
于 2012-07-26T01:06:25.173 回答