Java Tutorial/JSP/Form Data Validation — различия между версиями
Строка 17: | Строка 17: | ||
function checker() | function checker() | ||
{ | { | ||
− | var regExp1 = /^(\d{1,2}) | + | var regExp1 = /^(\d{1,2})-(\d{1,2})-(\d{2})$/ |
− | var regExp2 = /^(\d{1,2}) | + | var regExp2 = /^(\d{1,2})-(\d{1,2})-(\d{4})$/ |
var result1 = document.form1.text1.value.match(regExp1) | var result1 = document.form1.text1.value.match(regExp1) | ||
var result2 = document.form1.text1.value.match(regExp2) | var result2 = document.form1.text1.value.match(regExp2) | ||
if (result1 == null && result2 == null) { | if (result1 == null && result2 == null) { | ||
− | alert("Sorry, that's not a valid date.") | + | alert("Sorry, that's not a valid date. Valid date is dd-mm-yy or dd-mm-yyyy.") |
document.form1.text1.value = "" | document.form1.text1.value = "" | ||
return false | return false |
Текущая версия на 07:36, 23 февраля 2011
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. Valid date is dd-mm-yy or dd-mm-yyyy.")
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>