View Full Version : Working with forms

10-07-2008, 02:02 AM
I am working with javascript forms for an assignment. The goal is to be able to prompt for a input, check it, and then write it to the page or display it in a new pop-up window. So far I have the javascript correctly checking my data, but I can't figure out how to make it echo the results back.

In the script I have a few testing functions to make sure the input is the correct form. They look something like...
function isEmpty(elem) { } --checks to see if field empty
function isValid(elem) {} --checks to see if field correct
function validate(form){} --returns true if (isEmpty && isValid)

In the body i have some code, listed below, that handles my input.
<input type="text" size="30" name="name1" id="name1" onchange="if (isEmpty(this)) {isValid(this)}" /><br />
<input type="submit" />

I'm mostly just looking for a little guidance on how to declare an output function and where/how to call it.

The simple examples I have looked up all seem to deal with input that isn't verified, and I can't quite figure out how to make that example work with what I am doing.

10-07-2008, 05:30 AM
What do you mean by output function? I guess you mean an alert()

Anyway, please elaborate a little further, and show us the code and the markup so we could easily understand what your desire is.

10-07-2008, 06:01 AM
I have reworked a fair amount of my code since making my first post.. so I'll try and make a more complete example of what my code looks like. I was trying to follow some form examples that I had found, and I think I was making some things more complicated then needed. Here is some more streamlined code.


<script type="text/javascript">

function isValid(elem) {
var str= elem.value;
var re = </foo$/>
if (!str.match(re))
setTimeout("focuselement('" + elem.form.anem + "', '" + elem.name +"')",0;
return false;
return true;

function printData(?????) {
document.write("The entry was: ");


in the body I declare the following:

Enter an amount:
<input type="text" size="30" name="name1" id="name1" onchange="isValid(this)" /><br />
<input type="submit" onclick="printData()" />

This code is making sure my regular expression is in a correct form. It prints "The entry was: " so I know my function printData is doing at least something. However, it seems to me like I need to pass the validated regular expression to the printData(?????) function, that or I am typing the wrong thing in the document.write(?????) line.

10-07-2008, 07:15 AM
What are you trying to do? I'm confused.

Also, don't use document.write, use nodeValue to add new value on your node instead.