Java Tutorial/JSP/Form Data Validation
Validate Data Before Submit using Regular Expressions
index.jsp
<HTML>
<HEAD>
<TITLE>Verifying User Data</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function checker()
{
var regExp1 = /^(\d{1,2})\/(\d{1,2})\/(\d{2})$/
var regExp2 = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/
var result1 = document.form1.text1.value.match(regExp1)
var result2 = document.form1.text1.value.match(regExp2)
if (result1 == null && result2 == null) {
alert("Sorry, that"s not a valid date.")
document.form1.text1.value = ""
return false
} else {
document.form1.submit()
}
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<H1>Verifying User Data</H1>
<FORM NAME="form1" ACTION="basic.jsp" METHOD="POST" ONSUBMIT="return checker()">
Please enter a date:
<INPUT TYPE="TEXT" NAME="text1">
<INPUT TYPE="SUBMIT" value="Submit">
</FORM>
</BODY>
<HTML>