JavaScript无法验证

时间:2014-09-30 11:15:30

标签: javascript forms jsp validation alert

此代码有什么问题?

网页正在导入此外部JavaScript但是,如果用户输入错误,则不会显示警告消息框。

page.jsp:

   <form name="reg-form" action="ServletName" 
onsubmit="return validateForm()" method="POST"> 
<table>
     <tr>
    <td>
        <input name="organization" type="text" required>
    </td>
     </tr>
     <tr>
            <td>
            <input type="submit" value="Register">
            </td>
     </tr> 
  </table>
 </form>        

val.js:

function validateForm() 
{
var result=true;
var message="";

var orgnization=document.forms["reg-form"]["organization"].value;
if(orgnization.length<3)
{
    message+="\nName of Organization cannot be less than 3 characters long";
    result=false;
}
if(!result)
{
    alert(message);
}
return result;
}

1 个答案:

答案 0 :(得分:0)

试试这个:

 function validateForm() 
 {
 var result=true;
 var message="";

 var orgnization=document.getElementsByName["organization"][0].value;
 if(orgnization.length<3)
 {
   message +="\n Name of Organization cannot be less than 3 characters long";
   result=false;
 }
 if(!result)
 {
   alert(message);
 }
  return result;
}