Form validation date format is entered (yyyy-MM-dd) format

For chestnut:
There is a form, which has an input label its name date

 <form action="InsertServlet?opr=insert" name="form"  method="post">
    	<p>购买日期:<input type="text" name="date" ><span>yyyy-MM-dd格式</span></p>
    
    	<p><input type="button" value="提交" οnclick="check()"></p>
    </form>

Click submit when the trigger back to check () Click event
below to get the value date, and then by the regular expression tests whether the Date format:

function check(){
		var date=document.getElementsByName("date")[0].value;
		var r=new RegExp(/^(\d{4})-(0\d{1}|1[0-2])-(0\d{1}|[12]\d{1}|3[01])$/);
		
if(!(r.test(date))){		
			alert("时间格式不正确!");
		}else{		
	//调用提交事件的方法	window.location.href="javascript:document.form.submit()"; 
		}
	
	}
Published 108 original articles · won praise 46 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_44739706/article/details/104409502