Java Tutorial/JSP/Form Data Validation — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|||
Строка 22: | Строка 22: | ||
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 | + | alert("Sorry, that's not a valid date.") |
document.form1.text1.value = "" | document.form1.text1.value = "" | ||
return false | return false |
Версия 07:15, 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.")
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>