<html>
<head>
<title>Abakada Forms Tutorials: Form Input Validation</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<script language="JavaScript">
<!--
function validateForm ( form ) {
for ( var e = 0; e < form.elements.length; e ++ ) {
var el = form.elements [ e ];
if ( el.type == 'text' || el.type == 'textarea' ||
el.type == 'password' || el.type == 'file' ) {
if ( el.value == '' ) {
alert ( 'Please fill out the ' + el.name + ' field' );
el.focus ( );
return false;
}
}
else if ( el.type.indexOf ( 'select' ) != -1 ) {
if ( el.selectedIndex == -1 ) {
alert ( 'Please select a value for ' + el.name );
el.focus ( );
return false;
}
}
else if ( el.type == 'radio' ) {
var group = form [ el.name ];
var checked = false;
if ( !group.length )
checked = el.checked;
else
for ( var r = 0; r < group.length; r ++ )
if ( ( checked = group [ r ].checked ) )
break;
if ( !checked ) {
alert ( 'Please check one of the ' + el.name + ' buttons' );
el.focus ( );
return false;
}
}
else if ( el.type == 'checkbox' ) {
var group = form [ el.name ];
if ( group.length ) {
var checked = false;
for ( var r = 0; r < group.length; r ++ )
if ( ( checked = group [ r ].checked ) )
break;
if ( !checked ) {
alert ( 'Please check one of the ' + el.name + ' checkboxes' );
el.focus ( );
return false;
}
}
}
}
if ( confirm ( 'Success! The form will now exit and close this window.' ) ) self.close ( );
return true;
}
//-->
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>Abakada Forms Demo: <span class="hilite">Form Input Validation</span></h2></div>
<hr size=1 width=92%>
<center>
<form name="theForm" action="" onsubmit="return validateForm ( this )">
<table cellpadding=8 bgcolor="lightsteelblue" style="border: 2px inset">
<tr>
<td>Full Name:<br><input type="text" name="FullName"></td>
<td>Password:<br><input type="password" name="PassWord"></td></tr>
<tr align="center">
<td colspan="2">File to upload:<br><input type="file" name="FileUpLoad"></td></tr>
<tr align="center">
<td colspan="2">Comments:<br><textarea name="TextArea" rows="3" cols="40" wrap="soft"></textarea></td></tr>
<tr valign="top" align="center">
<td>Languages:<br><select name="Languages" multiple>
<option>English
<option>French
<option>German
<option>Russian
</select></td>
<td>Hobbies:<br><select name="Hobbies">
<option>Web
<option>Movies
<option>Concerts
<option>Pubs
<option>Books</select></td></tr>
<tr align="center">
<td colspan="2"><input type="radio" name="Gender" value="male">Male <input type="radio" name="Gender" value="female">Female</td></tr>
<tr align="center">
<td colspan="2"><input type="checkbox" name="Options" value="subscribe">Add my subscription <input type="checkbox" name="Options" value="cash">Send me cash too</td></tr>
<tr align="center">
<td colspan="2"><input type="submit" value="Submit"> <input type="Reset"></td></tr>
</table>
</form>
</center>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>