<!-- Part of javalessons.com (c)2007 SEMM(NL) All rights reserved Author:P.Hamaker For educational purposes only.-->
<html><head>
<style type="text/css">
.err
{font-style: italic;color: red;}
body{
font-family: sans-serif; font-size: large; margin-left: 20; margin-top: 30;
}
</style>
<script type="text/javascript">
var bok = true ;
function vname()
{
 var str, nm ;
 in1err.innerHTML='';
 str = document.getElementById('in1').value ;
 try {
 if ( str.length == 0 )  throw 'err-nm-1' ;
 
 var regexp = /[^a-zA-Z \'\-]/ ;
 if ( regexp.test( str ) )  throw 'err-nm-2' ;
 
 regexp = /[A-Z]/ ;
 if ( !regexp.test( str ) )  throw 'err-nm-3';
 }
 catch( e ) {
  if ( e == 'err-nm-1' )
     document.getElementById('in1err').innerHTML = 'fill in please' ;
  else   
     document.getElementById('in1err').innerHTML = 'wrong' ;
  bok = false;
 }
}
function vdate()
{
 var str, dt=null ;
 in2err.innerHTML='';
 str = document.getElementById('in2').value ;
 try {
 var arr = str.split( '/' );
 var dt = new Date( arr[0], arr[1] - 1, arr[2] );
 
 if ( arr[1] - 1  != dt.getMonth() || arr[2] != dt.getDate()
     || arr[0] != dt.getFullYear() )
    throw 'err-dt-1';
    
 var today = new Date() ;
 today.setHours( 0 );  today.setMinutes( 0 );
 today.setSeconds( 0 ); today.setMilliseconds( 0 );
 if ( dt < today )
    throw 'err-dt-2';
 }
 catch(e) {
  if ( e == 'err-dt-2' )
    document.getElementById('in2err').innerHTML = 'must be future date' ;
  else
    document.getElementById('in2err').innerHTML = 'wrong' ;
  bok = false;
 }
}
function vinc()
{
 var str ;
 in3err.innerHTML='';
 str = document.getElementById('in3').value ;
 try {
 var regexp = /[^0-9]/ ;
 if ( regexp.test( str ) )  throw 'err-nm-1' ;
 var n = parseInt( str );
 if ( isNaN( n ) )  throw 'err-inc-2' ;
 if ( n > 40000000 )  throw 'err-inc-3' ;
 }
 catch(e) {
  if ( e == 'err-inc-3' )
    document.getElementById('in3err').innerHTML = 'too high';
  else
    document.getElementById('in3err').innerHTML = 'wrong' ;
  bok = false; }
}
function validate()
{
 bok = true ;
 vname() ;
 vdate() ;
 vinc() ;
 return bok ;
}
function show(p)
{
 document.getElementById('ta').value += p + '\n';
//java.awt.Toolkit.getDefaultToolkit().beep();
}
</script>
</head>
<body>
JavaScript Lesson Form Validation
<!-- form action="/dataslurp/SomeServlet" ... -->
<br>
<form action=""
  onsubmit="return validate()">
<table><tr><td>

Last name :</td><td>
<input type="text" id="in1" maxlength="30" size="32">
<span id="in1err" class="err"></span>

</td></tr><tr><td>

Date to pass on :</td><td>
<input type="text" id="in2" maxlength="10" size="14" value="2040/12/31">
<span id="in2err" class="err"></span>

</td></tr><tr><td>

Desired yearly income :</td><td>
<input type="text" id="in3" maxlength="8" size="12" style="text-align: right;">
<span id="in3err" class="err"></span>

</td></tr>
<tr><td colspan="2">
<input type="submit" value="Send">
<input type="reset" value="Clear">
</td>  </tr>
</table>
</form>
<!--
<form>
<textarea id="ta" rows="20" cols="40" style="position: absolute;right: 10px;top: 10px"></textarea>
</form>
-->
<a href="http://javalessons.com" target="_top"><tt>JavaLessons.com</tt></a>
</body></html>