2013-07-09 4 views
0

JavaScript не может правильно проверить форму. Когда требуемое поле не вводится, а не отображается окно предупреждения, браузер просто загружается на следующую страницу, как если бы пользователь вводил это поле.Проверка формы не работает должным образом

<head> 
<script type="text/javascript"> 
    function inputValidation() { 
    var dateBox= document.forms["form"]["date"].value; 
    if (dateBox== null || dateBox== "") { 
     alert("Please enter an approximate date."); 
     return false; 
    } 
    } 
</script> 

</head> 
<body> 

    <form name="form" id="form" action="add.php" onsubmit="inputValidation()" method="post"> 
    <fieldset id="contactFields"> 
     <legend>Client Information</legend><br> 
     <p id="require"><span>*</span>Required information</p><br> 
     <table width="291" height="158"> 
     <tr> 
      <td>Approximate Date Serviced:<span>*</span></td> 
      <td><input type="text" name="date" id="date" size="20" maxlength="25"> 
     </tr> 

     <tr> 
      <td>First Name:</td> 
      <td><input type="text" name="fName" id="fName" size="20" maxlength="25" /></td> 
     </tr> 
     <tr> 
      <td>Last Name:</td> 
      <td><input type="text" name="lName" id="lName" size="20" maxlength="25"/></td> 
     </tr> 
     </table> 
     <label class="blockLabel">Comments:<span>*</span><br><textarea name = "comment" id="comment" rows="5" cols="55"></textarea> 

     <label id="wordcountLabel" for="wordcount"> 
     <div>Character Count:<input type="text" id="wordcount" readonly="readonly" value="0/0"></div> 
     </label> 
    </fieldset> 
    <div id="submit"><input text-align="center" class="button1" type="submit" value="Submit"></div> 
    <input text-align="center" class="button3" type="reset" value="Reset" /> 
    </form> 
</body> 

Любые идеи кто-нибудь? Пожалуйста помоги!

ответ

2

Предполагая, что код работает, и только проверка не делает - я думаю, что вы хотите что-то вроде следующего:

onsubmit="return inputValidation()" 

Полный код:

<head> 
<SCRIPT LANGUAGE="JavaScript" type="text/javascript"> 
function inputValidation() 
{ 
    var dateBox= document.forms["form"]["date"].value; 
    if (dateBox== null || dateBox== "") 
    { 
     alert("Please enter an approximate date."); 
     return false; 
    } 
    return true; 
} 
</script>  
</head> 
<body>  
<form name="form" id="form" action="add.php" onsubmit="return inputValidation()" method="post">  
    <fieldset id="contactFields">  
    <legend>Client Information</legend><br />  
<p id="require"><span>*</span>Required information</p><br />  
<label class="blockLabel">Approximate Date Serviced: <span>*</span><br/><input type="date" name = "date" id="date"/><br/><br/> 
<label class="blockLabel">First Name: <span>*</span><br/><input type="text" name = "firstName" id="firstName"/><br/><br/> 
<label class="blockLabel">Last Name: <span>*</span><br/><input type="text" name = "lastName" id="lastName"/><br/><br/> 

<label class="blockLabel">Comments:<span>*</span><br/><textarea name = "comment" id="comment" rows="5" cols="55"></textarea> 

<label id="wordcountLabel" for="wordcount"> 
<div>Character Count:<input type="text" id="wordcount" readonly="readonly" value="0/0" /></div> 
</label> 

</fieldset> 
    <div id="submit"><input text-align="center" class="button1" type="submit" value="Submit" /></div> 

<input text-align="center" class="button3" type="reset" value="Reset" /> 
</form> 
</body> 
+0

@RobG Я считаю, что мы согласны :) – alfasin

+0

@RobG теперь, когда вопрос был исправлен - так был ответ :) – alfasin

1

Вы пробовали отлаживать с помощью браузера и проверять значение dateBox ?? Я никогда не видел значение формы, на которое это ссылается, возможно, оно не получает нужного вам значения.

+0

Ссылки на значения формы «, как, что «был частью javascript с самого начала. Ваш ответ, вероятно, должен был быть комментарием, так как это не ответ. – RobG

+0

Я ценю ваш вклад, но, по крайней мере, способствую ЧТО-ТО, чтобы найти ответ. Для чего это место, верно? – Seano666

+0

То, что вы разместили в качестве ответа, более подходит в качестве комментария. Альфасин опубликовал (в основном правильный) ответ перед вашим, поэтому нет смысла вводить другой ответ. – RobG

Смежные вопросы