In article <ca**********@ctb-nnrp2.saix.net>,
"Gawie Marais" <gm*****@gam.co.za> wrote:
hi all,
i have a form and would like to check two fields.
if field 1 containes the letter 'c' and the other field contains
'undefined', then an error should apprear ina box on the screen.
This will get you started. You will need to add some style attributes
to get the box. It will only work for one message. To get different
messages to popup in the same place, you will need to dynamically alter
the document text or use multiple hidden divisions. I put up an alter
message for older browsers.
Robert
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
<head>
<title>Check form fields</title>
<style type="text/css">
..formStyle {position:relative; visibility:hidden;}
</style>
<script type="text/javascript">
var messageText =
"What a suprise! Please correct the error in your ways." +
" When you have the letter c in the name field, you must" +
" have something in the address field.";
function validate()
{
var x = document.forms["myForm"];
var submitOK = true;
alert(".theName.value = " + x.theName.value +
" ... .length = " + x.theName.value.length +
"\n" +
" .theAddress.value= " + x.theAddress.value +
" ... .length = " + x.theAddress.value.length );
if (x.theName.value.indexOf("c") >= 0 &&
x.theAddress.value.length == 0)
{
if (document.getElementById)
{
document.getElementById("errorMsg").style.visibili ty =
"visible";
}
else
{ alert(messageText); }
submitOK = false;
}
else
{
document.getElementById("errorMsg").style.visibili ty =
"hidden";
}
alert("submitOK= " + submitOK);
return submitOK;
}
</script>
</head>
<body>
<p>Please try out our form.</p>
<form name="myForm"
action="http://www.natAValidWebAddress.com"
method="POST"
onsubmit="return validate();">
<p>Name:<br>
<input type="text" name="theName" size="20"><br><br>
Address:<br>
<input type="text" name="theAddress" size="20"><br>
<script type="text/javascript">
//
// Only insert a div if we can change it
// Span didn't work in IE 5.2 for the Mac
//
if (document.getElementById)
{
var insertMessage = "<div id='errorMsg' class='formStyle'>" +
"<p>" + messageText +
"<\/p><\/div>";
document.write(insertMessage);
}
</script>
<p><input type="submit"></p>
</form>
<script type="text/javascript">
// This code needs to be after the form.
validate();
</script>
</body>
</html>