js 判断表单是否为空和是否是有效数字

判断是否为空和是否是有效数字

 
 
<s:form name='form' onsubmit="return myCheck()" method="post" action="add">
    <table>
            <tr>
                <td><s:textfield key="label.productName" name="product.name" /></td>
            </tr>
            <tr>
                <td><s:textfield key="label.productPrice" name="product.price" /></td>
            </tr>
            <tr>
                <td>
                        <s:submit key="label.add" ></s:submit>
                </td>
            </tr>
        </table>
</s:form>
<script type="text/javascript">
 function myCheck(form)
            {
               for(var i=0;i<document.form.elements.length-1;i++)
               {

                var vari = document.form.elements[i];
                if(vari.name==="product.price"&&vari){
                if(isNaN(vari.value)){
alert("price 不是数字");
        return false;
}
}
                  if(document.form.elements[i].value=="")
                  {
                     alert("当前表单不能有空项");
                     document.form.elements[i].focus();
                     return false;
                  }


               }
               return true;
              
            }

</script>
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/jiangfeilong/p/10889349.html