Click to See Complete Forum and Search --> : question about document.write
snoopy0877
10-26-2003, 07:34 PM
I have a form and one javascript function. When I submit or click on submit button, then it will call javascript function, in javascript I write "document.write('You must fill all field')". I want that text write in bottom of the form but when document.write the text my form disappear in this page????
Help me pls....thks
You cannot use document.write() after the page has loaded; instead, you must append a text node to an block-level element in the document.
[J]ona
snoopy0877
10-27-2003, 12:45 AM
Can you show me how? I want to check valid a form after user input and document.write the notice in its page if them input a invalid value.
Charles
10-27-2003, 03:51 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 verify (form) {
var submit = true;
if (!/\S/.test(form.name.value)) {submit = false; form.name.previousSibling.data = '* ' + form.name.previousSibling.data; form.name.parentNode.style.color = '#f00'};
if (!/\S/.test(form.address.value)) {submit = false; form.address.previousSibling.data = '* ' + form.address.previousSibling.data; form.address.parentNode.style.color = '#f00'};
if (!/\S/.test(form.phoneNumber.value)) {submit = false; form.phoneNumber.previousSibling.data = '* ' + form.phoneNumber.previousSibling.data; form.phoneNumber.parentNode.style.color = '#f00'};
if (!submit) {
document.getElementById('message').firstChild.data = 'Items with a "*" are required.';
}
return submit;
}
// -->
</script>
<form action="" onsubmit="return verify(this)">
<div>
<p id="message" style="color:#f00"> </p>
<label>Name<input id="name" type="text"></label>
<label>Address<input id="address" type="text"></label>
<label>Phone Number<input id="phoneNumber" type="text"></label>
<button type="submit">Submit</button>
</div>
</form>
And see http://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/ for details.
snoopy0877
10-27-2003, 08:34 PM
Thanks Charles. But can you explain /\S/.test(form.name.value) syntax pls.
document.getElementById('message').firstChild.data = 'Items with a "*" are required.';
firstChild.data not done in IE, I replace with innerHTML ;-)
You shouldn't use innerHTML, because it is not a standards-complaint property and is not listed at the W3C, and therefore should not be used.
As for that syntax, it is a Regular Expression. See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/guide/regexp.html for more information on RegExps.
[J]ona