Click to See Complete Forum and Search --> : pls. help - form validation
chin_chin
11-13-2003, 06:11 AM
Pls. help me. I need a javascript code that will validate if the user didn't put any value in a text box.
if the information in the form is complete then all of the info will be displayed on another page...... if not, an error message will be displayed on the first page (the page where the user fills-up all the information.)
Charles
11-13-2003, 06:25 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>
<script type="text/javascript">
<!--
function require () {for (var i=1; i<arguments.length; i++) {if (arguments[0][arguments[i]].type == 'text' && !/\S/.test(arguments[0][arguments[i]].value)) {alert (['Field', arguments[0][arguments[i]].previousSibling.data, 'is required.'].join(' ')); arguments[0][arguments[i]].focus(); return false}}}
// -->
</script>
<form action="" onsubmit="return require(this, 'name', 'rank', 'serialNumber')">
<div>
<label>Name<input id="name" type="text"></label>
<label>Rank<input id="rank" type="text"></label>
<label>Serial Number<input id="serialNumber" type="text"></label>
<button type="submit">Submit</button>
</div>
</form>