Click to See Complete Forum and Search --> : Question when submitting a form


gjack72
12-01-2005, 02:25 PM
:rolleyes: How do I go about making sure all of my text fields have data before the form will submit? Do I need to add something to my form tag?
I want a message to display if everything is not filled in when the button is clicked.

Thanks

GJ

<form name="reg" id="reg" action="thankyou.html" method="get" target="new">

saulss
12-01-2005, 02:29 PM
you need to use some javascript to check that
like...

function check()
{
if(document.getElementById('reg').value=='')
{
alert("empty field");
return false;
}
else
{
return true;
}
}

in your form tag add this

onsubmit="return check()">

Charles
12-01-2005, 02:39 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">

<script type="text/javascript">

function check (f) {
var e, i = 0
while (e = f.elements[i++]) {
if (e.type == 'text' && !/\S/.test (e.value)) {
alert ('Please, field "' + e.parentNode.firstChild.data + '" is required.')
e.focus()
return false
}
}
}

</script>

<style type="text/css">

form {margin:auto; width:20em}
fieldset {padding:1ex}
label {display:block; text-align:right}
input {margin-left:1ex}
button {display:block; margin:auto}

</style>

</head>
<body>
<form action="some-script.pl" onsubmit="return check (this)">
<fieldset>
<legend>Example</legend>
<label>Name<input type="text"></label>
<label>Rank<input type="text"></label>
<label>Serial number<input type="text"></label>
<button type="submit">Submit</button>
</fieldset>
</form>
</body>
</html>

gjack72
12-01-2005, 02:47 PM
Thanks for the info.
It is now working correctly.

GJ