日期:2014-05-16  浏览次数:20351 次

js form 提交验证

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

  <script type="text/javascript" src="../../js/jquery-1.4.2.min.js"></script>
  <script type="text/javascript">
  	$(function(){
  		$("#name").keyup(function(){
  		 	var obj = $(this);
  			$.getJSON("searchDishByNameAjax",{name:$(this).val()},function(data){
  				if(data == "1"){
  					var name = obj.val();
  					if($("#add").length >0){
  						$("#add").html(name+"已存在,请输入其它菜名。");
  						return;
  					}
  					$("<div/>").css("border","solid 1px #FF0000").attr("id","add").html(name+"已存在,请输入其它菜名。").insertAfter(obj);
  				}else{
  					$("#add").remove();
  				}
  			});
  			var name = $(this).val(); 
  		});
  		//下面表示只可以输入数字和一个点号
  		$("#price").keydown(function(event){
  			var code = event.keyCode;
  			if((code >= 48 && code <= 57) || code == 110 || code == 190){
  				if((code == 110 || code == 190) && $(this).val().indexOf(".") >= 0){
  					return false;
  				}
  				return true;
  			}
  			else{ 
  				event.keyCode = 0;
   				return false;
  			}
  		}) 
  	});
  	
  	function onSubmit(){ 
  		if($("#add").length>0)
  			return false;
  		$("#addDish")[0].submit();		
  	};
  </script>
  </head>
  
  <body>
     <form id = "addDish" action = "addDish" method = "post" ">
		<table>
			<tr>
				<th>
					菜名
				</th>
				<td>
					<input type = "text" id = "name" name = "dish.name"  />
				</td>
			</tr>
			<tr>
				<th>
					菜价
				</th>
				<td>
					<input type = "text" id = "price" name = "dish.price" maxlength = "8"/>
				</td>
			</tr>
			<tr>
				<th>
					说明
				</th>
				<td>
					<textarea  id = "comm" name="dish.comm" style="margin-left: 2px; margin-right: 2px; width: 540px; margin-top: 2px; margin-bottom: 2px; height: 184px; ">						 
					</textarea>
				</td>
			</tr>
			<tr>
				<td></td>
				<td>
					<span style = "margin-left:20px;text-decoration : underline;" onclick = "onSubmit();">确定</span>
					<span style = "margin-left:20px;text-decoration : underline;"><a href = "showDish">返回</a></span>
				</td>
			</tr>
		</table>     
     </form>
  </body>
</html>
?